Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@adiwajshing/baileys/lib/Utils/crypto.js
1129 views
1
"use strict";
2
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
if (k2 === undefined) k2 = k;
4
var desc = Object.getOwnPropertyDescriptor(m, k);
5
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
desc = { enumerable: true, get: function() { return m[k]; } };
7
}
8
Object.defineProperty(o, k2, desc);
9
}) : (function(o, m, k, k2) {
10
if (k2 === undefined) k2 = k;
11
o[k2] = m[k];
12
}));
13
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
Object.defineProperty(o, "default", { enumerable: true, value: v });
15
}) : function(o, v) {
16
o["default"] = v;
17
});
18
var __importStar = (this && this.__importStar) || function (mod) {
19
if (mod && mod.__esModule) return mod;
20
var result = {};
21
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
__setModuleDefault(result, mod);
23
return result;
24
};
25
var __importDefault = (this && this.__importDefault) || function (mod) {
26
return (mod && mod.__esModule) ? mod : { "default": mod };
27
};
28
Object.defineProperty(exports, "__esModule", { value: true });
29
exports.hkdf = exports.sha256 = exports.hmacSign = exports.aesEncrypWithIV = exports.aesEncrypt = exports.aesDecryptWithIV = exports.aesDecrypt = exports.aesDecryptGCM = exports.aesEncryptGCM = exports.signedKeyPair = exports.Curve = exports.generateSignalPubKey = void 0;
30
const crypto_1 = require("crypto");
31
const futoin_hkdf_1 = __importDefault(require("futoin-hkdf"));
32
const libsignal = __importStar(require("libsignal"));
33
const Defaults_1 = require("../Defaults");
34
/** prefix version byte to the pub keys, required for some curve crypto functions */
35
const generateSignalPubKey = (pubKey) => (pubKey.length === 33
36
? pubKey
37
: Buffer.concat([Defaults_1.KEY_BUNDLE_TYPE, pubKey]));
38
exports.generateSignalPubKey = generateSignalPubKey;
39
exports.Curve = {
40
generateKeyPair: () => {
41
const { pubKey, privKey } = libsignal.curve.generateKeyPair();
42
return {
43
private: Buffer.from(privKey),
44
// remove version byte
45
public: Buffer.from(pubKey.slice(1))
46
};
47
},
48
sharedKey: (privateKey, publicKey) => {
49
const shared = libsignal.curve.calculateAgreement((0, exports.generateSignalPubKey)(publicKey), privateKey);
50
return Buffer.from(shared);
51
},
52
sign: (privateKey, buf) => (libsignal.curve.calculateSignature(privateKey, buf)),
53
verify: (pubKey, message, signature) => {
54
try {
55
libsignal.curve.verifySignature((0, exports.generateSignalPubKey)(pubKey), message, signature);
56
return true;
57
}
58
catch (error) {
59
return false;
60
}
61
}
62
};
63
const signedKeyPair = (identityKeyPair, keyId) => {
64
const preKey = exports.Curve.generateKeyPair();
65
const pubKey = (0, exports.generateSignalPubKey)(preKey.public);
66
const signature = exports.Curve.sign(identityKeyPair.private, pubKey);
67
return { keyPair: preKey, signature, keyId };
68
};
69
exports.signedKeyPair = signedKeyPair;
70
const GCM_TAG_LENGTH = 128 >> 3;
71
/**
72
* encrypt AES 256 GCM;
73
* where the tag tag is suffixed to the ciphertext
74
* */
75
function aesEncryptGCM(plaintext, key, iv, additionalData) {
76
const cipher = (0, crypto_1.createCipheriv)('aes-256-gcm', key, iv);
77
cipher.setAAD(additionalData);
78
return Buffer.concat([cipher.update(plaintext), cipher.final(), cipher.getAuthTag()]);
79
}
80
exports.aesEncryptGCM = aesEncryptGCM;
81
/**
82
* decrypt AES 256 GCM;
83
* where the auth tag is suffixed to the ciphertext
84
* */
85
function aesDecryptGCM(ciphertext, key, iv, additionalData) {
86
const decipher = (0, crypto_1.createDecipheriv)('aes-256-gcm', key, iv);
87
// decrypt additional adata
88
const enc = ciphertext.slice(0, ciphertext.length - GCM_TAG_LENGTH);
89
const tag = ciphertext.slice(ciphertext.length - GCM_TAG_LENGTH);
90
// set additional data
91
decipher.setAAD(additionalData);
92
decipher.setAuthTag(tag);
93
return Buffer.concat([decipher.update(enc), decipher.final()]);
94
}
95
exports.aesDecryptGCM = aesDecryptGCM;
96
/** decrypt AES 256 CBC; where the IV is prefixed to the buffer */
97
function aesDecrypt(buffer, key) {
98
return aesDecryptWithIV(buffer.slice(16, buffer.length), key, buffer.slice(0, 16));
99
}
100
exports.aesDecrypt = aesDecrypt;
101
/** decrypt AES 256 CBC */
102
function aesDecryptWithIV(buffer, key, IV) {
103
const aes = (0, crypto_1.createDecipheriv)('aes-256-cbc', key, IV);
104
return Buffer.concat([aes.update(buffer), aes.final()]);
105
}
106
exports.aesDecryptWithIV = aesDecryptWithIV;
107
// encrypt AES 256 CBC; where a random IV is prefixed to the buffer
108
function aesEncrypt(buffer, key) {
109
const IV = (0, crypto_1.randomBytes)(16);
110
const aes = (0, crypto_1.createCipheriv)('aes-256-cbc', key, IV);
111
return Buffer.concat([IV, aes.update(buffer), aes.final()]); // prefix IV to the buffer
112
}
113
exports.aesEncrypt = aesEncrypt;
114
// encrypt AES 256 CBC with a given IV
115
function aesEncrypWithIV(buffer, key, IV) {
116
const aes = (0, crypto_1.createCipheriv)('aes-256-cbc', key, IV);
117
return Buffer.concat([aes.update(buffer), aes.final()]); // prefix IV to the buffer
118
}
119
exports.aesEncrypWithIV = aesEncrypWithIV;
120
// sign HMAC using SHA 256
121
function hmacSign(buffer, key, variant = 'sha256') {
122
return (0, crypto_1.createHmac)(variant, key).update(buffer).digest();
123
}
124
exports.hmacSign = hmacSign;
125
function sha256(buffer) {
126
return (0, crypto_1.createHash)('sha256').update(buffer).digest();
127
}
128
exports.sha256 = sha256;
129
// HKDF key expansion
130
function hkdf(buffer, expandedLength, info) {
131
return (0, futoin_hkdf_1.default)(!Buffer.isBuffer(buffer) ? Buffer.from(buffer) : buffer, expandedLength, info);
132
}
133
exports.hkdf = hkdf;
134
135