Use non-subtle crypto in libsignal-protocol

This commit is contained in:
Fedor Indutny 2021-03-23 17:50:02 -07:00 committed by GitHub
parent 9e9d1c8e84
commit 919259c960
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 120 additions and 13 deletions

View file

@ -0,0 +1,57 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import crypto from 'crypto';
import { typedArrayToArrayBuffer as toArrayBuffer } from '../Crypto';
export function sign(key: ArrayBuffer, data: ArrayBuffer): ArrayBuffer {
return toArrayBuffer(
crypto
.createHmac('sha256', Buffer.from(key))
.update(Buffer.from(data))
.digest()
);
}
export function hash(data: ArrayBuffer): ArrayBuffer {
return toArrayBuffer(
crypto.createHash('sha512').update(Buffer.from(data)).digest()
);
}
export function encrypt(
key: ArrayBuffer,
data: ArrayBuffer,
iv: ArrayBuffer
): ArrayBuffer {
const cipher = crypto.createCipheriv(
'aes-256-cbc',
Buffer.from(key),
Buffer.from(iv)
);
const encrypted = Buffer.concat([
cipher.update(Buffer.from(data)),
cipher.final(),
]);
return toArrayBuffer(encrypted);
}
export function decrypt(
key: ArrayBuffer,
data: ArrayBuffer,
iv: ArrayBuffer
): ArrayBuffer {
const cipher = crypto.createDecipheriv(
'aes-256-cbc',
Buffer.from(key),
Buffer.from(iv)
);
const decrypted = Buffer.concat([
cipher.update(Buffer.from(data)),
cipher.final(),
]);
return toArrayBuffer(decrypted);
}