mirror of
https://github.com/openpgpjs/openpgpjs.git
synced 2025-06-13 09:46:38 +00:00

As specified in openpgp-crypto-refresh-09. Instead of encoding the symmetric key algorithm in the PKESK ciphertext (requiring padding), the symmetric key algorithm is left unencrypted. Co-authored-by: Lukas Burkhalter <lukas.burkhalter@proton.ch>
22 lines
758 B
JavaScript
22 lines
758 B
JavaScript
/**
|
|
* @fileoverview This module implements HKDF using either the WebCrypto API or Node.js' crypto API.
|
|
* @module crypto/hkdf
|
|
* @private
|
|
*/
|
|
|
|
import enums from '../enums';
|
|
import util from '../util';
|
|
|
|
const webCrypto = util.getWebCrypto();
|
|
const nodeCrypto = util.getNodeCrypto();
|
|
|
|
export default async function HKDF(hashAlgo, key, salt, info, length) {
|
|
const hash = enums.read(enums.webHash, hashAlgo);
|
|
if (!hash) throw new Error('Hash algo not supported with HKDF');
|
|
|
|
const crypto = webCrypto || nodeCrypto.webcrypto.subtle;
|
|
const importedKey = await crypto.importKey('raw', key, 'HKDF', false, ['deriveBits']);
|
|
const bits = await crypto.deriveBits({ name: 'HKDF', hash, salt, info }, importedKey, length * 8);
|
|
return new Uint8Array(bits);
|
|
}
|