Path: blob/master/node_modules/@adiwajshing/baileys/lib/Utils/crypto.js
2593 views
"use strict";1var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {2if (k2 === undefined) k2 = k;3Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });4}) : (function(o, m, k, k2) {5if (k2 === undefined) k2 = k;6o[k2] = m[k];7}));8var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {9Object.defineProperty(o, "default", { enumerable: true, value: v });10}) : function(o, v) {11o["default"] = v;12});13var __importStar = (this && this.__importStar) || function (mod) {14if (mod && mod.__esModule) return mod;15var result = {};16if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);17__setModuleDefault(result, mod);18return result;19};20Object.defineProperty(exports, "__esModule", { value: true });21exports.hkdf = exports.sha256 = exports.hmacSign = exports.aesEncrypWithIV = exports.aesEncrypt = exports.aesDecryptWithIV = exports.aesDecrypt = exports.signedKeyPair = exports.Curve = void 0;22const crypto_1 = require("crypto");23const curveJs = __importStar(require("curve25519-js"));24exports.Curve = {25generateKeyPair: () => {26const { public: pubKey, private: privKey } = curveJs.generateKeyPair(crypto_1.randomBytes(32));27return {28private: Buffer.from(privKey),29public: Buffer.from(pubKey)30};31},32sharedKey: (privateKey, publicKey) => {33const shared = curveJs.sharedKey(privateKey, publicKey);34return Buffer.from(shared);35},36sign: (privateKey, buf) => (Buffer.from(curveJs.sign(privateKey, buf, null))),37verify: (pubKey, message, signature) => {38return curveJs.verify(pubKey, message, signature);39}40};41const signedKeyPair = (keyPair, keyId) => {42const signKeys = exports.Curve.generateKeyPair();43const pubKey = new Uint8Array(33);44pubKey.set([5], 0);45pubKey.set(signKeys.public, 1);46const signature = exports.Curve.sign(keyPair.private, pubKey);47return { keyPair: signKeys, signature, keyId };48};49exports.signedKeyPair = signedKeyPair;50/** decrypt AES 256 CBC; where the IV is prefixed to the buffer */51function aesDecrypt(buffer, key) {52return aesDecryptWithIV(buffer.slice(16, buffer.length), key, buffer.slice(0, 16));53}54exports.aesDecrypt = aesDecrypt;55/** decrypt AES 256 CBC */56function aesDecryptWithIV(buffer, key, IV) {57const aes = crypto_1.createDecipheriv('aes-256-cbc', key, IV);58return Buffer.concat([aes.update(buffer), aes.final()]);59}60exports.aesDecryptWithIV = aesDecryptWithIV;61// encrypt AES 256 CBC; where a random IV is prefixed to the buffer62function aesEncrypt(buffer, key) {63const IV = crypto_1.randomBytes(16);64const aes = crypto_1.createCipheriv('aes-256-cbc', key, IV);65return Buffer.concat([IV, aes.update(buffer), aes.final()]); // prefix IV to the buffer66}67exports.aesEncrypt = aesEncrypt;68// encrypt AES 256 CBC with a given IV69function aesEncrypWithIV(buffer, key, IV) {70const aes = crypto_1.createCipheriv('aes-256-cbc', key, IV);71return Buffer.concat([aes.update(buffer), aes.final()]); // prefix IV to the buffer72}73exports.aesEncrypWithIV = aesEncrypWithIV;74// sign HMAC using SHA 25675function hmacSign(buffer, key, variant = 'sha256') {76return crypto_1.createHmac(variant, key).update(buffer).digest();77}78exports.hmacSign = hmacSign;79function sha256(buffer) {80return crypto_1.createHash('sha256').update(buffer).digest();81}82exports.sha256 = sha256;83// HKDF key expansion84// from: https://github.com/benadida/node-hkdf85function hkdf(buffer, expandedLength, { info, salt }) {86const hashAlg = 'sha256';87const hashLength = 32;88salt = salt || Buffer.alloc(hashLength);89// now we compute the PRK90const prk = crypto_1.createHmac(hashAlg, salt).update(buffer).digest();91let prev = Buffer.from([]);92const buffers = [];93const num_blocks = Math.ceil(expandedLength / hashLength);94const infoBuff = Buffer.from(info || []);95for (var i = 0; i < num_blocks; i++) {96const hmac = crypto_1.createHmac(hashAlg, prk);97// XXX is there a more optimal way to build up buffers?98const input = Buffer.concat([99prev,100infoBuff,101Buffer.from(String.fromCharCode(i + 1))102]);103hmac.update(input);104prev = hmac.digest();105buffers.push(prev);106}107return Buffer.concat(buffers, expandedLength);108}109exports.hkdf = hkdf;110111112