mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
feat: use futoin-hkdf instead of custom function
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user