feat: use futoin-hkdf instead of custom function

This commit is contained in:
Adhiraj Singh
2022-04-18 08:22:43 +05:30
parent a4ae3526de
commit 5200bf6477
2 changed files with 4 additions and 29 deletions

View File

@@ -38,6 +38,7 @@
"@hapi/boom": "^9.1.3",
"axios": "^0.24.0",
"curve25519-js": "^0.0.4",
"futoin-hkdf": "^1.5.0",
"libsignal": "git+https://github.com/adiwajshing/libsignal-node",
"music-metadata": "^7.4.1",
"node-cache": "^5.1.2",

View File

@@ -1,5 +1,6 @@
import { createCipheriv, createDecipheriv, createHash, createHmac, randomBytes } from 'crypto'
import * as curveJs from 'curve25519-js'
import HKDF from 'futoin-hkdf'
import { KeyPair } from '../Types'
export const Curve = {
@@ -67,33 +68,6 @@ export function sha256(buffer: Buffer) {
}
// HKDF key expansion
// from: https://github.com/benadida/node-hkdf
export function hkdf(buffer: Uint8Array, expandedLength: number, { info, salt }: { salt?: Buffer, info?: string }) {
const hashAlg = 'sha256'
const hashLength = 32
salt = salt || Buffer.alloc(hashLength)
// now we compute the PRK
const prk = createHmac(hashAlg, salt).update(buffer).digest()
let prev = Buffer.from([])
const buffers = []
const num_blocks = Math.ceil(expandedLength / hashLength)
const infoBuff = Buffer.from(info || [])
for(var i = 0; i < num_blocks; i++) {
const hmac = createHmac(hashAlg, prk)
// XXX is there a more optimal way to build up buffers?
const input = Buffer.concat([
prev,
infoBuff,
Buffer.from(String.fromCharCode(i + 1))
])
hmac.update(input)
prev = hmac.digest()
buffers.push(prev)
}
return Buffer.concat(buffers, expandedLength)
export function hkdf(buffer: Uint8Array | Buffer, expandedLength: number, info: { salt?: Buffer, info?: string }) {
return HKDF(!Buffer.isBuffer(buffer) ? Buffer.from(buffer) : buffer, expandedLength, info)
}