hs.hashing
ModuleModule for hashing and encoding operations
Every function that takes data accepts it as bytes, in any of these forms:
- a
Uint8Array(or any other typed array) or anArrayBuffer - an array of numbers from 0 to 255
- a string, which is used as its UTF-8 encoding
Anything else throws an error, rather than quietly hashing some other bytes.
Properties
This module has no properties.
Methods
hs.hashing.base64Encode(data) -> string
Encode bytes as base64
hs.hashing.base64Encode(data) -> string
| Name | Type | Description |
|---|---|---|
| data | Uint8Array | ArrayBuffer | number[] | string | The bytes to encode. A string is encoded as UTF-8 |
console.log(hs.hashing.base64Encode("Hello"))
console.log(hs.hashing.base64Encode(new Uint8Array([0x00, 0xff])))
hs.hashing.base64Decode(data) -> string
Decode a base64 string
hs.hashing.base64Decode(data) -> string
| Name | Type | Description |
|---|---|---|
| data | string | The base64 string to decode |
console.log(hs.hashing.base64Decode("SGVsbG8="))
hs.hashing.base32Encode(data) -> string
Encode bytes as base32 (RFC 4648), with = padding
hs.hashing.base32Encode(data) -> string
| Name | Type | Description |
|---|---|---|
| data | Uint8Array | ArrayBuffer | number[] | string | The bytes to encode. A string is encoded as UTF-8 |
console.log(hs.hashing.base32Encode("foobar")) // "MZXW6YTBOI======"
console.log(hs.hashing.base32Encode([0x48, 0x65, 0x6c, 0x6c, 0x6f])) // "JBSWY3DP"
hs.hashing.base32Decode(text) -> Uint8Array | null
Decode a base32 string (RFC 4648) into bytes
Decoding is lenient in the ways people write base32 secrets: letters can be upper or lower case,
and spaces, - and = padding are ignored. Any other character makes the text invalid.
hs.hashing.base32Decode(text) -> Uint8Array | null
| Name | Type | Description |
|---|---|---|
| text | string | The base32 text to decode |
const key = hs.hashing.base32Decode("JBSW Y3DP EHPK 3PXP")
console.log(key.length + " bytes")
hs.hashing.md5(data) -> string
Generate the MD5 hash of some bytes
hs.hashing.md5(data) -> string
| Name | Type | Description |
|---|---|---|
| data | Uint8Array | ArrayBuffer | number[] | string | The bytes to hash. A string is hashed as UTF-8 |
console.log(hs.hashing.md5("hello"))
hs.hashing.sha1(data) -> string
Generate the SHA1 hash of some bytes
hs.hashing.sha1(data) -> string
| Name | Type | Description |
|---|---|---|
| data | Uint8Array | ArrayBuffer | number[] | string | The bytes to hash. A string is hashed as UTF-8 |
console.log(hs.hashing.sha1("hello"))
hs.hashing.sha256(data) -> string
Generate the SHA256 hash of some bytes
hs.hashing.sha256(data) -> string
| Name | Type | Description |
|---|---|---|
| data | Uint8Array | ArrayBuffer | number[] | string | The bytes to hash. A string is hashed as UTF-8 |
console.log(hs.hashing.sha256("hello"))
console.log(hs.hashing.sha256(new Uint8Array([0x00, 0xff, 0x80])))
hs.hashing.sha512(data) -> string
Generate the SHA512 hash of some bytes
hs.hashing.sha512(data) -> string
| Name | Type | Description |
|---|---|---|
| data | Uint8Array | ArrayBuffer | number[] | string | The bytes to hash. A string is hashed as UTF-8 |
console.log(hs.hashing.sha512("hello"))
hs.hashing.digest(algorithm, data) -> Uint8Array
Generate the hash of some bytes, as bytes
hs.hashing.digest(algorithm, data) -> Uint8Array
| Name | Type | Description |
|---|---|---|
| algorithm | "MD5" | "SHA1" | "SHA256" | "SHA512" | The hash algorithm |
| data | Uint8Array | ArrayBuffer | number[] | string | The bytes to hash. A string is hashed as UTF-8 |
const hash = hs.hashing.digest("SHA256", "hello")
console.log("First byte: " + hash[0])
hs.hashing.hmacMD5(key, data) -> string
Generate the HMAC-MD5 of some bytes with a key
hs.hashing.hmacMD5(key, data) -> string
| Name | Type | Description |
|---|---|---|
| key | Uint8Array | ArrayBuffer | number[] | string | The secret key. A string is used as UTF-8 |
| data | Uint8Array | ArrayBuffer | number[] | string | The data to authenticate. A string is used as UTF-8 |
console.log(hs.hashing.hmacMD5("secret", "hello"))
hs.hashing.hmacSHA1(key, data) -> string
Generate the HMAC-SHA1 of some bytes with a key
hs.hashing.hmacSHA1(key, data) -> string
| Name | Type | Description |
|---|---|---|
| key | Uint8Array | ArrayBuffer | number[] | string | The secret key. A string is used as UTF-8 |
| data | Uint8Array | ArrayBuffer | number[] | string | The data to authenticate. A string is used as UTF-8 |
console.log(hs.hashing.hmacSHA1("secret", "hello"))
hs.hashing.hmacSHA256(key, data) -> string
Generate the HMAC-SHA256 of some bytes with a key
hs.hashing.hmacSHA256(key, data) -> string
| Name | Type | Description |
|---|---|---|
| key | Uint8Array | ArrayBuffer | number[] | string | The secret key. A string is used as UTF-8 |
| data | Uint8Array | ArrayBuffer | number[] | string | The data to authenticate. A string is used as UTF-8 |
console.log(hs.hashing.hmacSHA256("secret", "hello"))
hs.hashing.hmacSHA512(key, data) -> string
Generate the HMAC-SHA512 of some bytes with a key
hs.hashing.hmacSHA512(key, data) -> string
| Name | Type | Description |
|---|---|---|
| key | Uint8Array | ArrayBuffer | number[] | string | The secret key. A string is used as UTF-8 |
| data | Uint8Array | ArrayBuffer | number[] | string | The data to authenticate. A string is used as UTF-8 |
console.log(hs.hashing.hmacSHA512("secret", "hello"))
hs.hashing.hmac(algorithm, key, data) -> Uint8Array
Generate the HMAC (RFC 2104) of some bytes with a key, as bytes
hs.hashing.hmac(algorithm, key, data) -> Uint8Array
| Name | Type | Description |
|---|---|---|
| algorithm | "MD5" | "SHA1" | "SHA256" | "SHA512" | The hash algorithm |
| key | Uint8Array | ArrayBuffer | number[] | string | The secret key. A string is used as UTF-8 |
| data | Uint8Array | ArrayBuffer | number[] | string | The data to authenticate. A string is used as UTF-8 |
// An HOTP code (RFC 4226)
const key = hs.hashing.base32Decode("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ")
const counter = new Uint8Array(8)
new DataView(counter.buffer).setBigUint64(0, 1n)
const mac = hs.hashing.hmac("SHA1", key, counter)
const offset = mac[mac.length - 1] & 0x0f
const binary = new DataView(mac.buffer, mac.byteOffset).getUint32(offset) & 0x7fffffff
console.log(String(binary % 1000000).padStart(6, "0"))