ballerina/crypto module
Module overview
This module 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
import ballerina/io;
import ballerina/crypto;
public function main() {
// 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 module. |
|
| Encoding | UTF8 | HEX | BASE64 | The encoding types supported for the HMAC key, by this module. |
Functions Summary
| Return Type | Function and Description | ||
|---|---|---|---|
| string | crc32(any content) Returns the CRC32 hash for the provided element. This accepts |
||
| string | hash(string baseString, SHA1|SHA256|MD5 algorithm) Returns the hash of the given string using the specified algorithm. |
||
| string | hmac(string baseString, string keyString, SHA1|SHA256|MD5 algorithm, UTF-8|BASE64|HEX? keyEncoding) Returns the HMAC value of the provided base string. |
Constants
public function crc32(any content) returns (string)
Returns the CRC32 hash for the provided element. This accepts string, byte[], 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, SHA1|SHA256|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 | SHA1|SHA256|MD5 | The hashing algorithm to be used |
| Return Type | Description | ||
|---|---|---|---|
| string | The hashed string |
public function hmac(string baseString, string keyString, SHA1|SHA256|MD5 algorithm, UTF-8|BASE64|HEX? keyEncoding) 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 | SHA1|SHA256|MD5 | The hashing algorithm to be used |
|
| keyEncoding | UTF-8|BASE64|HEX? | null | The encoding of the key |
| Return Type | Description | ||
|---|---|---|---|
| string | The hashed string |