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 createHmac (data: Buffer, key: Buffer): Promise {
const hmac = crypto.createHmac(HMAC_ALGORITHM, key)
hmac.update(data)
const hex = hmac.digest('hex')
const result = convertHexToBuffer(hex)
return result
}
): Promise<
IJsonRpcRequest | IJsonRpcResponseSuccess | IJsonRpcResponseError | null
> {
const _key: Buffer = convertArrayBufferToBuffer(key)
if (!_key) {
throw new Error('Missing key: required for decryption')
}
const verified: boolean = await verifyHmac(payload, _key)
if (!verified) {
return null
}
const cipherText: Buffer = convertHexToBuffer(payload.data)
const iv: Buffer = convertHexToBuffer(payload.iv)
const buffer: Buffer = await aesCbcDecrypt(cipherText, _key, iv)
const utf8: string = convertBufferToUtf8(buffer)
let data: IJsonRpcRequest
try {
data = JSON.parse(utf8)
} catch (error) {
return null
}
return data
}
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 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 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
}
key: ArrayBuffer
): Promise<
IJsonRpcRequest | IJsonRpcResponseSuccess | IJsonRpcResponseError | null
> {
const _key: Buffer = convertArrayBufferToBuffer(key)
if (!_key) {
throw new Error('Missing key: required for decryption')
}
const verified: boolean = await verifyHmac(payload, _key)
if (!verified) {
return null
}
const cipherText: Buffer = convertHexToBuffer(payload.data)
const iv: Buffer = convertHexToBuffer(payload.iv)
const buffer: Buffer = await aesCbcDecrypt(cipherText, _key, iv)
const utf8: string = convertBufferToUtf8(buffer)
let data: IJsonRpcRequest
try {
data = JSON.parse(utf8)
} catch (error) {
return null
}
return data
}