MCPcopy Create free account
hub / github.com/codetheweb/tuyapi / _encrypt35

Method _encrypt35

lib/cipher.js:96–112  ·  view source on GitHub ↗

* Encrypt data for protocol 3.5 * @param {Object} options Options for encryption * @param {String} options.data data to encrypt * @param {Boolean} [options.base64=true] `true` to return result in Base64 * @returns {Buffer|String} returns Buffer unless options.base64 is true

(options)

Source from the content-addressed store, hash-verified

94 * @returns {Buffer|String} returns Buffer unless options.base64 is true
95 */
96 _encrypt35(options) {
97 let encrypted;
98 let localIV = Buffer.from((Date.now() * 10).toString().slice(0, 12));
99 if (options.iv !== undefined) {
100 localIV = options.iv.slice(0, 12);
101 }
102
103 const cipher = crypto.createCipheriv('aes-128-gcm', this.getKey(), localIV);
104 if (options.aad === undefined) {
105 encrypted = Buffer.concat([cipher.update(options.data), cipher.final()]);
106 } else {
107 cipher.setAAD(options.aad);
108 encrypted = Buffer.concat([localIV, cipher.update(options.data), cipher.final(), cipher.getAuthTag(), Buffer.from([0x00, 0x00, 0x99, 0x66])]);
109 }
110
111 return encrypted;
112 }
113
114 /**
115 * Decrypts data.

Callers 2

encryptMethod · 0.95
_packetHandlerMethod · 0.80

Calls 1

getKeyMethod · 0.95

Tested by

no test coverage detected