@microsoft/dev-tunnels-ssh
Version:
SSH library for Dev Tunnels
69 lines • 2.83 kB
JavaScript
;
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeHmac = void 0;
const crypto = require("crypto");
const buffer_1 = require("buffer");
const hmacAlgorithm_1 = require("../hmacAlgorithm");
class NodeHmac extends hmacAlgorithm_1.HmacAlgorithm {
constructor(name, algorithmName, encryptThenMac = false) {
super(name, algorithmName, NodeHmac.getHashKeyLength(algorithmName), NodeHmac.getHashDigestLength(algorithmName));
this.encryptThenMac = encryptThenMac;
}
async createSigner(key) {
const hmac = new NodeSignerVerifier(NodeHmac.getNodeHashAlgorithmName(this.algorithmName), this.digestLength, this.encryptThenMac, key);
return hmac;
}
async createVerifier(key) {
const hmac = new NodeSignerVerifier(NodeHmac.getNodeHashAlgorithmName(this.algorithmName), this.digestLength, this.encryptThenMac, key);
return hmac;
}
static getHashKeyLength(hashAlgorithmName) {
if (hashAlgorithmName === 'SHA2-512')
return 512 / 8;
if (hashAlgorithmName === 'SHA2-384')
return 384 / 8;
if (hashAlgorithmName === 'SHA2-256')
return 256 / 8;
throw new Error(`Unsupported hash algorithm: ${hashAlgorithmName}`);
}
static getHashDigestLength(hashAlgorithmName) {
return this.getHashKeyLength(hashAlgorithmName);
}
static getNodeHashAlgorithmName(hashAlgorithmName) {
if (hashAlgorithmName === 'SHA2-512')
return 'sha512';
if (hashAlgorithmName === 'SHA2-384')
return 'sha384';
if (hashAlgorithmName === 'SHA2-256')
return 'sha256';
throw new Error(`Unsupported hash algorithm: ${hashAlgorithmName}`);
}
}
exports.NodeHmac = NodeHmac;
class NodeSignerVerifier {
constructor(algorithmName, digestLength, encryptThenMac, key) {
this.algorithmName = algorithmName;
this.digestLength = digestLength;
this.encryptThenMac = encryptThenMac;
// crypto.createSecretKey is only available on node >= 11.6.
this.key = crypto.createSecretKey ? crypto.createSecretKey(key) : buffer_1.Buffer.from(key);
}
async sign(data) {
const signer = crypto.createHmac(this.algorithmName, this.key);
signer.update(data);
const hmac = signer.digest();
return hmac;
}
async verify(data, signature) {
const verifier = crypto.createHmac(this.algorithmName, this.key);
verifier.update(data);
const hmac = verifier.digest();
const result = hmac.equals(signature);
return result;
}
dispose() { }
}
//# sourceMappingURL=nodeHmac.js.map