Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export async function encrypt (
data: IJsonRpcRequest | IJsonRpcResponseSuccess | IJsonRpcResponseError,
key: ArrayBuffer
): Promise {
const _key: Buffer = convertArrayBufferToBuffer(key)
const ivArrayBuffer: ArrayBuffer = await generateKey(128)
const iv: Buffer = convertArrayBufferToBuffer(ivArrayBuffer)
const ivHex: string = convertBufferToHex(iv, true)
const contentString: string = JSON.stringify(data)
const content: Buffer = convertUtf8ToBuffer(contentString)
const cipherText: Buffer = await aesCbcEncrypt(content, _key, iv)
const cipherTextHex: string = convertBufferToHex(cipherText, true)
const unsigned: Buffer = concatBuffers(cipherText, iv)
const hmac: Buffer = await createHmac(unsigned, _key)
const hmacHex: string = convertBufferToHex(hmac, true)
return {
data: cipherTextHex,
hmac: hmacHex,
iv: ivHex
}
export async function verifyHmac (
payload: IEncryptionPayload,
key: Buffer
): Promise {
const cipherText: Buffer = convertHexToBuffer(payload.data)
const iv: Buffer = convertHexToBuffer(payload.iv)
const hmac: Buffer = convertHexToBuffer(payload.hmac)
const hmacHex: string = convertBufferToHex(hmac, true)
const unsigned: Buffer = concatBuffers(cipherText, iv)
const chmac: Buffer = await createHmac(unsigned, key)
const chmacHex: string = convertBufferToHex(chmac, true)
if (removeHexPrefix(hmacHex) === removeHexPrefix(chmacHex)) {
return true
}
return false
}
export async function encrypt (
data: IJsonRpcRequest | IJsonRpcResponseSuccess | IJsonRpcResponseError,
key: ArrayBuffer
): Promise {
const _key: Buffer = convertArrayBufferToBuffer(key)
const ivArrayBuffer: ArrayBuffer = await generateKey(128)
const iv: Buffer = convertArrayBufferToBuffer(ivArrayBuffer)
const ivHex: string = convertBufferToHex(iv, true)
const contentString: string = JSON.stringify(data)
const content: Buffer = convertUtf8ToBuffer(contentString)
const cipherText: Buffer = await aesCbcEncrypt(content, _key, iv)
const cipherTextHex: string = convertBufferToHex(cipherText, true)
const unsigned: Buffer = concatBuffers(cipherText, iv)
const hmac: Buffer = await createHmac(unsigned, _key)
const hmacHex: string = convertBufferToHex(hmac, true)
return {
data: cipherTextHex,
hmac: hmacHex,
iv: ivHex
}
}
): Promise {
const _key: Buffer = convertArrayBufferToBuffer(key)
const ivArrayBuffer: ArrayBuffer = await generateKey(128)
const iv: Buffer = convertArrayBufferToBuffer(ivArrayBuffer)
const ivHex: string = convertBufferToHex(iv, true)
const contentString: string = JSON.stringify(data)
const content: Buffer = convertUtf8ToBuffer(contentString)
const cipherText: Buffer = await aesCbcEncrypt(content, _key, iv)
const cipherTextHex: string = convertBufferToHex(cipherText, true)
const unsigned: Buffer = concatBuffers(cipherText, iv)
const hmac: Buffer = await createHmac(unsigned, _key)
const hmacHex: string = convertBufferToHex(hmac, true)
return {
data: cipherTextHex,
hmac: hmacHex,
iv: ivHex
}
}
export async function generateKey (length?: number): Promise {
const _length = (length || 256) / 8
const buffer: Buffer = await randomBytes(_length)
const hex = convertBufferToHex(buffer, true)
const result = convertHexToArrayBuffer(hex)
return result
}
export async function verifyHmac (
payload: IEncryptionPayload,
key: Buffer
): Promise {
const cipherText: Buffer = convertHexToBuffer(payload.data)
const iv: Buffer = convertHexToBuffer(payload.iv)
const hmac: Buffer = convertHexToBuffer(payload.hmac)
const hmacHex: string = convertBufferToHex(hmac, true)
const unsigned: Buffer = concatBuffers(cipherText, iv)
const chmac: Buffer = await createHmac(unsigned, key)
const chmacHex: string = convertBufferToHex(chmac, true)
if (removeHexPrefix(hmacHex) === removeHexPrefix(chmacHex)) {
return true
}
return false
}