xiot-core-xcp-ts
Version:
27 lines (26 loc) • 1.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebSocketBinaryFrameCodecImpl = void 0;
const chacha20poly1305_1 = require("@stablelib/chacha20poly1305");
const buffer_1 = require("buffer");
class WebSocketBinaryFrameCodecImpl {
constructor(deviceToServer, serverToDevice) {
this.deviceToServer = deviceToServer;
this.serverToDevice = serverToDevice;
this.inCount = 0;
this.outCount = 0;
}
encrypt(data) {
const cc = new chacha20poly1305_1.ChaCha20Poly1305(this.deviceToServer);
const nonce = new buffer_1.Buffer(8);
nonce.writeInt32LE(this.inCount++, 0);
return cc.seal(nonce, data);
}
decrypt(data) {
const cc = new chacha20poly1305_1.ChaCha20Poly1305(this.serverToDevice);
const nonce = new buffer_1.Buffer(8);
nonce.writeInt32LE(this.outCount++, 0);
return cc.open(nonce, data);
}
}
exports.WebSocketBinaryFrameCodecImpl = WebSocketBinaryFrameCodecImpl;