Helios API/ API Reference/ Namespaces/

Crypto

The Helios Crypto namespace contains a collection of cryptography primitives.

These functions have been implemented as part of the Helios library in order to avoid external dependencies (there still isn't a standardized Javascript Crypto API that provides all the needed functionality).

Index

Functions

blake2b

blake2b(bytes, digestSize?): number[]

Calculates blake2b hash of a list of uint8 numbers (variable digest size). Result is also a list of uint8 numbers.

Parameters

NameTypeDescription
bytesnumber[]
digestSize?numberDefaults to 32. Can't be greater than 64.

Returns

number[]

List of uint8 numbers.

Example

bytesToHex(Crypto.blake2b([0, 1])) == "01cf79da4945c370c68b265ef70641aaa65eaa8f5953e3900d97724c2c5aa095"

Example

bytesToHex(Crypto.blake2b(textToBytes("abc"), 64)) == "ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923"

decodeBase32

decodeBase32(encoded, alphabet?): number[]

Decodes a Base32 string into bytes.

Parameters

NameTypeDescription
encodedstring
alphabet?stringlist of chars, defaults to "abcdefghijklmnopqrstuvwxyz234567"

Returns

number[]

Example

bytesToText(Crypto.decodeBase32("my")) == "f"

Example

bytesToText(Crypto.decodeBase32("mzxq")) == "fo"

Example

bytesToText(Crypto.decodeBase32("mzxw6")) == "foo"

Example

bytesToText(Crypto.decodeBase32("mzxw6yq")) == "foob"

Example

bytesToText(Crypto.decodeBase32("mzxw6ytb")) == "fooba"

Example

bytesToText(Crypto.decodeBase32("mzxw6ytboi")) == "foobar"

decodeBech32

decodeBech32(addr): [string, number[]]

Decomposes a Bech32 checksummed string (eg. a Cardano address), and returns the human readable part and the original bytes Throws an error if checksum is invalid.

Parameters

NameType
addrstring

Returns

[string, number[]]

First part is the human-readable part, second part is a list containing the underlying bytes.

Example

bytesToHex(Crypto.decodeBech32("addr_test1wz54prcptnaullpa3zkyc8ynfddc954m9qw5v3nj7mzf2wggs2uld")[1]) == "70a9508f015cfbcffc3d88ac4c1c934b5b82d2bb281d464672f6c49539"

encodeBase32

encodeBase32(bytes, alphabet?): string

Encodes bytes in using Base32.

Parameters

NameTypeDescription
bytesnumber[]list of uint8 numbers
alphabet?stringlist of chars, defaults to "abcdefghijklmnopqrstuvwxyz234567"

Returns

string

Example

Crypto.encodeBase32(textToBytes("f")) == "my"

Example

Crypto.encodeBase32(textToBytes("fo")) == "mzxq"

Example

Crypto.encodeBase32(textToBytes("foo")) == "mzxw6"

Example

Crypto.encodeBase32(textToBytes("foob")) == "mzxw6yq"

Example

Crypto.encodeBase32(textToBytes("fooba")) == "mzxw6ytb"

Example

Crypto.encodeBase32(textToBytes("foobar")) == "mzxw6ytboi"

encodeBech32

encodeBech32(hrp, data): string

Creates a Bech32 checksummed string (eg. used to represent Cardano addresses).

Parameters

NameTypeDescription
hrpstringhuman-readable part (eg. "addr")
datanumber[]a list of uint8 bytes

Returns

string

Example

Crypto.encodeBech32("foo", textToBytes("foobar")) == "foo1vehk7cnpwgry9h96"

Example

Crypto.encodeBech32("addr_test", hexToBytes("70a9508f015cfbcffc3d88ac4c1c934b5b82d2bb281d464672f6c49539")) == "addr_test1wz54prcptnaullpa3zkyc8ynfddc954m9qw5v3nj7mzf2wggs2uld"

hmacSha2_256

hmacSha2_256(key, message): number[]

Hmac using sha2-256.

Parameters

NameType
keynumber[]
messagenumber[]

Returns

number[]

Example

bytesToHex(Crypto.hmacSha2_256(textToBytes("key"), textToBytes("The quick brown fox jumps over the lazy dog"))) == "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8"

hmacSha2_512

hmacSha2_512(key, message): number[]

Hmac using sha2-512.

Parameters

NameType
keynumber[]
messagenumber[]

Returns

number[]

Example

bytesToHex(Crypto.hmacSha2_512(textToBytes("key"), textToBytes("The quick brown fox jumps over the lazy dog"))) == "b42af09057bac1e2d41708e48a902e09b5ff7f12ab428a4fe86653c73dd248fb82f948a549f7b791a5b41915ee4d1ec3935357e4e2317250d0372afa2ebeeb3a"

mulberry32

mulberry32(seed): NumberGenerator

A simple pseudo-random number generator for use in tests that requires some randomness but need to be deterministic (i.e. each test run gives the same result).

Parameters

NameType
seednumber

Returns

NumberGenerator

The returned function returns a new random number between 0 and 1 upon each call.

pbkdf2

pbkdf2(prf, password, salt, iters, dkLength): number[]

Password-Based Key Derivation Function 2.

Parameters

NameType
prf(key: number[], msg: number[]) => number[]
passwordnumber[]
saltnumber[]
itersnumber
dkLengthnumber

Returns

number[]

Example

bytesToHex(Crypto.pbkdf2(Crypto.hmacSha2_256, textToBytes("password"), textToBytes("salt"), 1, 20)) == "120fb6cffcf8b32c43e7225256c4f837a86548c9"

Example

bytesToHex(Crypto.pbkdf2(Crypto.hmacSha2_512, textToBytes("password"), textToBytes("salt"), 2, 20)) == "e1d9c16aa681708a45f5c7c4e215ceb66e011a2e"

rand

rand(seed): NumberGenerator

Alias for mulberry32.

Parameters

NameType
seednumber

Returns

NumberGenerator

The returned function returns a new random number between 0 and 1 upon each call.

sha2_256

sha2_256(bytes): number[]

Calculates sha2-256 (32bytes) hash of a list of bytes. Result is also a list of bytes.

Parameters

NameTypeDescription
bytesnumber[]List of uint8 numbers

Returns

number[]

List of uint8 numbers.

Example

bytesToHex(Crypto.sha2_256([0x61, 0x62, 0x63])) == "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"

Example

Crypto.sha2_256(textToBytes("Hello, World!")) == [223, 253, 96, 33, 187, 43, 213, 176, 175, 103, 98, 144, 128, 158, 195, 165, 49, 145, 221, 129, 199, 247, 10, 75, 40, 104, 138, 54, 33, 130, 152, 111]

sha2_512

sha2_512(bytes): number[]

Calculates sha2-512 (64bytes) hash of a list of uint8 numbers. Result is also a list of uint8 number.

Parameters

NameTypeDescription
bytesnumber[]List of uint8 numbers

Returns

number[]

List of uint8 numbers.

Example

bytesToHex(Crypto.sha2_512([0x61, 0x62, 0x63])) == "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"

Example

bytesToHex(Crypto.sha2_512([])) == "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"

Example

bytesToHex(Crypto.sha2_512(textToBytes("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"))) == "204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c33596fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445"

Example

bytesToHex(Crypto.sha2_512(textToBytes("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstuu"))) == "23565d109ac0e2aa9fb162385178895058b28489a6bc31cb55491ed83956851ab1d4bbd46440586f5c9c4b69c9c280118cbc55c71495d258cc27cc6bb25ee720"

sha3

sha3(bytes): number[]

Calculates sha3-256 (32bytes) hash of a list of uint8 numbers. Result is also a list of uint8 number.

Parameters

NameTypeDescription
bytesnumber[]List of uint8 numbers

Returns

number[]

List of uint8 numbers.

Example

bytesToHex(Crypto.sha3(textToBytes("abc"))) == "3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532"

Example

bytesToHex(Crypto.sha3((new Array(136)).fill(1))) == "b36dc2167c4d9dda1a58b87046c8d76a6359afe3612c4de8a38857e09117b2db"

Example

bytesToHex(Crypto.sha3((new Array(135)).fill(2))) == "5bdf5d815d29a9d7161c66520efc17c2edd7898f2b99a029e8d2e4ff153407f4"

Example

bytesToHex(Crypto.sha3((new Array(134)).fill(3))) == "8e6575663dfb75a88f94a32c5b363c410278b65020734560d968aadd6896a621"

Example

bytesToHex(Crypto.sha3((new Array(137)).fill(4))) == "f10b39c3e455006aa42120b9751faa0f35c821211c9d086beb28bf3c4134c6c6"

verifyBech32

verifyBech32(encoded): boolean

Verifies a Bech32 checksum.

Parameters

NameType
encodedstring

Returns

boolean

Example

Crypto.verifyBech32("foo1vehk7cnpwgry9h96") == true

Example

Crypto.verifyBech32("foo1vehk7cnpwgry9h97") == false

Example

Crypto.verifyBech32("a12uel5l") == true

Example

Crypto.verifyBech32("mm1crxm3i") == false

Example

Crypto.verifyBech32("A1G7SGD8") == false

Example

Crypto.verifyBech32("abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw") == true

Example

Crypto.verifyBech32("?1ezyfcl") == true

Example

Crypto.verifyBech32("addr_test1wz54prcptnaullpa3zkyc8ynfddc954m9qw5v3nj7mzf2wggs2uld") == true