Skip to main content
Version: v3+

MD5 and SHA Digests

createCrypto

Create a digest instance. Pass the HMAC key through private_key.

| algorithmId | Description | API_LEVEL | | ------------------ | ------------------- | --------- | --- | | alg.MD5 | MD5 digest | 3.0 | | alg.HMACMD5 | HMAC-MD5 digest | 3.0 | | alg.SHA_256 | SHA-256 digest | 3.0 | | alg.HMAC_SHA_256 | HMAC-SHA-256 digest | 3.0 | | alg.SHA_1 | SHA-1 digest | 3.0 | | alg.HMAC_SHA_1 | HMAC-SHA-1 digest | 3.0 | . |

Type

function createCrypto(
algorithmId: typeof alg.MD5 | typeof alg.SHA_256 | typeof alg.SHA_1,
option?: DigestOptions,
): DigestCrypto | undefined
function createCrypto(
algorithmId: typeof alg.HMACMD5 | typeof alg.HMAC_SHA_256 | typeof alg.HMAC_SHA_1,
option: HMACOptions,
): DigestCrypto | undefined

Parameters

DigestData

TypeDescription
number[]|ArrayBuffer|Uint8ArrayData to digest

DigestOptions

PropertyTypeRequiredDefaultValueDescription
key_encryptboolean|numberN-Whether the key is encrypted again by the hardware key

HMACOptions

PropertyTypeRequiredDefaultValueDescription
private_keyDigestDataY-Key used by HMAC algorithms
key_encryptboolean|numberN-Whether the key is encrypted again by the hardware key

DigestCrypto

MD5, SHA, and HMAC digest instance supporting one-shot and streaming calculation.

Methods

encrypt

Calculate a digest in one call

encrypt(data: createCrypto.DigestData): DigestResult | undefined
DigestResult
PropertyTypeDescription
md_contentArrayBufferDigest data
lengthnumberDigest length

start

Start streaming digest calculation

start(): void

update

Append a data chunk

update(data: createCrypto.DigestData): void

finish

Finish streaming calculation and return the digest

finish(): DigestResult | undefined
DigestResult
PropertyTypeDescription
md_contentArrayBufferDigest data
lengthnumberDigest length

Example

import { alg, createCrypto } from '@zos/crypto'

const digest = createCrypto(alg.HMAC_SHA_256, { private_key: [1, 2, 3] })
if (!digest) throw new Error('Failed to create digest instance')

const oneShot = digest.encrypt([65, 66, 67])

digest.start()
digest.update([65])
digest.update([66, 67])
const streamed = digest.finish()