mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-04-25 14:32:18 +00:00
Test encryption and decryption
This commit is contained in:
parent
16990f3de6
commit
1861f42728
17
src/xx.ts
17
src/xx.ts
@ -81,8 +81,8 @@ export class XXHandshake {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private nonceToBytes(n: uint32) : bytes {
|
private nonceToBytes(n: uint32) : bytes {
|
||||||
const nonce = Buffer.alloc(12);
|
const nonce = Buffer.alloc(12, 0x00);
|
||||||
nonce.writeUInt32LE(n, 4);
|
nonce.writeUInt32LE(n, 4, true);
|
||||||
|
|
||||||
return nonce;
|
return nonce;
|
||||||
}
|
}
|
||||||
@ -90,22 +90,23 @@ export class XXHandshake {
|
|||||||
private encrypt(k: bytes32, n: uint32, ad: bytes, plaintext: bytes) : bytes {
|
private encrypt(k: bytes32, n: uint32, ad: bytes, plaintext: bytes) : bytes {
|
||||||
const nonce = this.nonceToBytes(n);
|
const nonce = this.nonceToBytes(n);
|
||||||
const ctx = new AEAD();
|
const ctx = new AEAD();
|
||||||
|
|
||||||
ctx.init(k, nonce);
|
ctx.init(k, nonce);
|
||||||
ctx.aad(ad);
|
ctx.aad(ad);
|
||||||
ctx.encrypt(plaintext);
|
ctx.encrypt(plaintext);
|
||||||
|
|
||||||
return ctx.final();
|
return plaintext;
|
||||||
}
|
}
|
||||||
|
|
||||||
private decrypt(k: bytes32, n: uint32, ad: bytes, ciphertext: bytes) : bytes {
|
private decrypt(k: bytes32, n: uint32, ad: bytes, ciphertext: bytes) : bytes {
|
||||||
const nonce = this.nonceToBytes(n);
|
const nonce = this.nonceToBytes(n);
|
||||||
const ctx = new AEAD();
|
const aead = new AEAD();
|
||||||
|
|
||||||
ctx.init(k, nonce);
|
aead.init(k, nonce);
|
||||||
ctx.aad(ad);
|
aead.aad(ad);
|
||||||
ctx.decrypt(ciphertext);
|
aead.decrypt(ciphertext);
|
||||||
|
|
||||||
return ctx.final();
|
return ciphertext;
|
||||||
}
|
}
|
||||||
|
|
||||||
private isEmptyKey(k: bytes32) : boolean {
|
private isEmptyKey(k: bytes32) : boolean {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { expect, assert } from "chai";
|
import { expect, assert } from "chai";
|
||||||
import { Buffer } from 'buffer';
|
import { Buffer } from 'buffer';
|
||||||
import { ed25519 } from 'bcrypto';
|
import { ed25519, AEAD } from 'bcrypto';
|
||||||
|
|
||||||
import { XXHandshake, KeyPair } from "../src/xx";
|
import { XXHandshake, KeyPair } from "../src/xx";
|
||||||
import { loadPayloadProto, generateEd25519Keys } from "./utils";
|
import { loadPayloadProto, generateEd25519Keys } from "./utils";
|
||||||
@ -108,4 +108,15 @@ describe("Index", () => {
|
|||||||
await doHandshake(xx);
|
await doHandshake(xx);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Test symmetric encrypt and decrypt", async () => {
|
||||||
|
const xx = new XXHandshake();
|
||||||
|
const { nsInit, nsResp } = await doHandshake(xx);
|
||||||
|
const ad = Buffer.from("authenticated");
|
||||||
|
const message = Buffer.from("HelloCrypto");
|
||||||
|
|
||||||
|
xx.encryptWithAd(nsInit.cs1, ad, message);
|
||||||
|
const decrypted = xx.decryptWithAd(nsResp.cs1, ad, message);
|
||||||
|
|
||||||
|
assert(Buffer.from("HelloCrypto").equals(decrypted), "Decrypted text buffer not equal to plaintext buffer");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user