ballerina/crypto package

Package overview

This packages provides the necessary utilities that are required to hash content using different hashing mechanisms and algorithms.

Hashing string content

The hash function uses an algorithm to hash content of type string.

Calculating HMAC

The hmac function uses an algorithm to calculate HMAC of a given string, using a given key.

Calculating CRC

The crc32 function can be used to calculate CRC on input types such as string, xml , json, and blobs.

Samples

Sample hash function

import ballerina/io;
import ballerina/crypto;

function main(string[] args) {
     // The string content to be hashed.
     string input = "Hello Ballerina";
     // The key used for hashing when required.
     string key = "somesecret";
     // The XML content to be hashed.
     xml xmlContent = xml `<foo>Hello Ballerina</foo>`;

     io:println("HMAC with MD5: " + crypto:hmac(input, key, crypto:MD5));
     io:println("HMAC with SHA1: " + crypto:hmac(input, key, crypto:SHA1));
     io:println("HMAC with SHA256: " + crypto:hmac(input, key, crypto:SHA256));

     io:println("Hash with MD5: " + crypto:hash(input, crypto:MD5));
     io:println("Hash with SHA1: " + crypto:hash(input, crypto:SHA1));
     io:println("Hash with SHA256: " + crypto:hash(input, crypto:SHA256));

     io:println("Hash with CRC32 for text: " + crypto:crc32(input));
     io:println("Hash with CRC32 for xml content: " + crypto:crc32(xmlContent));
}

Type Definitions

Type Values Description
Algorithm SHA256 | SHA1 | MD5

The hashing algorithms supported by this package.

Functions Summary

Return Type Function and Description
string crc32(any content)

Returns the CRC32 hash for the provided element. This accepts string, blob, json and xml content.

string hash(string baseString, SHA256|SHA1|MD5 algorithm)

Returns the hash of the given string using the specified algorithm.

string hmac(string baseString, string keyString, SHA256|SHA1|MD5 algorithm)

Returns the HMAC value of the provided base string.

Global Variables

Name Data Type Description
MD5 Algorithm

The MD5 hashing algorithm

SHA1 Algorithm

The SHA1 hashing algorithm"

SHA256 Algorithm

The SHA256 hashing algorithm

public function crc32(any content) returns (string)

Returns the CRC32 hash for the provided element. This accepts string, blob, json and xml content.

Parameter Name Data Type Default Value Description
content any

The content to be hashed

Return Type Description
string

The generated hash

public function hash(string baseString, SHA256|SHA1|MD5 algorithm) returns (string)

Returns the hash of the given string using the specified algorithm.

Parameter Name Data Type Default Value Description
baseString string

The string to be hashed

algorithm SHA256|SHA1|MD5

The hashing algorithm to be used

Return Type Description
string

The hashed string

public function hmac(string baseString, string keyString, SHA256|SHA1|MD5 algorithm) returns (string)

Returns the HMAC value of the provided base string.

Parameter Name Data Type Default Value Description
baseString string

The string to be hashed

keyString string

The key string

algorithm SHA256|SHA1|MD5

The hashing algorithm to be used

Return Type Description
string

The hashed string