Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
payload: IEncryptionPayload,
key: ArrayBuffer
): Promise<
IJsonRpcRequest | IJsonRpcResponseSuccess | IJsonRpcResponseError | null
> {
if (!key) {
throw new Error('Missing key: required for decryption')
}
const verified: boolean = await verifyHmac(payload, key)
if (!verified) {
return null
}
const cipherText: ArrayBuffer = convertHexToArrayBuffer(payload.data)
const iv: ArrayBuffer = convertHexToArrayBuffer(payload.iv)
const buffer: ArrayBuffer = await aesCbcDecrypt(cipherText, key, iv)
const utf8: string = convertArrayBufferToUtf8(buffer)
let data: IJsonRpcRequest
try {
data = JSON.parse(utf8)
} catch (error) {
return null
}
return data
}
export async function verifyHmac (
payload: IEncryptionPayload,
key: ArrayBuffer
): Promise {
const cipherText: ArrayBuffer = convertHexToArrayBuffer(payload.data)
const iv: ArrayBuffer = convertHexToArrayBuffer(payload.iv)
const hmac: ArrayBuffer = convertHexToArrayBuffer(payload.hmac)
const hmacHex: string = convertArrayBufferToHex(hmac, true)
const unsigned: ArrayBuffer = concatArrayBuffers(cipherText, iv)
const chmac: ArrayBuffer = await createHmac(unsigned, key)
const chmacHex: string = convertArrayBufferToHex(chmac, true)
if (removeHexPrefix(hmacHex) === removeHexPrefix(chmacHex)) {
return true
}
return false
}
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 decrypt (
payload: IEncryptionPayload,
key: ArrayBuffer
): Promise<
IJsonRpcRequest | IJsonRpcResponseSuccess | IJsonRpcResponseError | null
> {
if (!key) {
throw new Error('Missing key: required for decryption')
}
const verified: boolean = await verifyHmac(payload, key)
if (!verified) {
return null
}
const cipherText: ArrayBuffer = convertHexToArrayBuffer(payload.data)
const iv: ArrayBuffer = convertHexToArrayBuffer(payload.iv)
const buffer: ArrayBuffer = await aesCbcDecrypt(cipherText, key, iv)
const utf8: string = convertArrayBufferToUtf8(buffer)
let data: IJsonRpcRequest
try {
data = JSON.parse(utf8)
} catch (error) {
return null
}
return data
}
export async function verifyHmac (
payload: IEncryptionPayload,
key: ArrayBuffer
): Promise {
const cipherText: ArrayBuffer = convertHexToArrayBuffer(payload.data)
const iv: ArrayBuffer = convertHexToArrayBuffer(payload.iv)
const hmac: ArrayBuffer = convertHexToArrayBuffer(payload.hmac)
const hmacHex: string = convertArrayBufferToHex(hmac, true)
const unsigned: ArrayBuffer = concatArrayBuffers(cipherText, iv)
const chmac: ArrayBuffer = await createHmac(unsigned, key)
const chmacHex: string = convertArrayBufferToHex(chmac, true)
if (removeHexPrefix(hmacHex) === removeHexPrefix(chmacHex)) {
return true
}
return false
}
set key (value: string) {
if (!value) {
return
}
const key: ArrayBuffer = convertHexToArrayBuffer(value)
this._key = key
}
export async function verifyHmac (
payload: IEncryptionPayload,
key: ArrayBuffer
): Promise {
const cipherText: ArrayBuffer = convertHexToArrayBuffer(payload.data)
const iv: ArrayBuffer = convertHexToArrayBuffer(payload.iv)
const hmac: ArrayBuffer = convertHexToArrayBuffer(payload.hmac)
const hmacHex: string = convertArrayBufferToHex(hmac, true)
const unsigned: ArrayBuffer = concatArrayBuffers(cipherText, iv)
const chmac: ArrayBuffer = await createHmac(unsigned, key)
const chmacHex: string = convertArrayBufferToHex(chmac, true)
if (removeHexPrefix(hmacHex) === removeHexPrefix(chmacHex)) {
return true
}
return false
}