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