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));
}

Package contents

Type Definitions

Type Values Description
Algorithm SHA256 | SHA1 | MD5

The algorithms which can be used in crypto functions.

Functions Summary

Return Type Function and Description
string crc32(any content)

Returns the CRC32 hash for the provided element. Currently supports strings and blobs.

string hash(string baseString, Algorithm algorithm)

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

string hmac(string baseString, string keyString, Algorithm algorithm)

Returns the HMAC value of the provided base string.

Global Variables

Name Data Type Description
MD5 Algorithm
SHA1 Algorithm
SHA256 Algorithm

public function crc32(any content) returns (string)

Returns the CRC32 hash for the provided element. Currently supports strings and blobs.

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, Algorithm 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 Algorithm

The hashing algorithm to be used

Return Type Description
string

The hashed string

public function hmac(string baseString, string keyString, Algorithm 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 Algorithm

The hashing algorithm to be used

Return Type Description
string

The hashed string