ecdh test
This commit is contained in:
parent
7732504136
commit
2fe9b83876
@ -9,6 +9,7 @@
|
||||
"author": "Sam Hadow",
|
||||
"license": "BSD-3-Clause-Attribution",
|
||||
"dependencies": {
|
||||
"@noble/curves": "^1.8.1",
|
||||
"bootstrap": "^5.3.3",
|
||||
"connect-sqlite3": "^0.9.15",
|
||||
"cookie-parser": "^1.4.7",
|
||||
|
@ -0,0 +1,15 @@
|
||||
// X25519 aka ECDH on Curve25519 from [RFC7748](https://www.rfc-editor.org/rfc/rfc7748)
|
||||
import { x25519 } from '@noble/curves/ed25519';
|
||||
|
||||
export function genKeys() {
|
||||
const priv = x25519.utils.randomPrivateKey();
|
||||
const pub = x25519.getPublicKey(priv);
|
||||
return {
|
||||
privkey: priv,
|
||||
pubkey: pub
|
||||
};
|
||||
}
|
||||
|
||||
export function sharedKey(priv, pub) {
|
||||
return x25519.getSharedSecret(priv, pub);
|
||||
}
|
16
tests/ecdh.test.js
Normal file
16
tests/ecdh.test.js
Normal file
@ -0,0 +1,16 @@
|
||||
import { genKeys, sharedKey } from '../src/public/ecdh.js';
|
||||
import { arrayToHex } from '../src/stringutils.js';
|
||||
|
||||
describe('ecdh.js functions', () => {
|
||||
|
||||
it('key exchange test', () => {
|
||||
const keysA= genKeys();
|
||||
const keysB = genKeys();
|
||||
const sharedA = sharedKey(keysA.privkey, keysB.pubkey);
|
||||
const sharedB = sharedKey(keysB.privkey, keysA.pubkey);
|
||||
const sharedAhex = arrayToHex(sharedA);
|
||||
const sharedBhex = arrayToHex(sharedB);
|
||||
expect(sharedAhex === sharedBhex);
|
||||
});
|
||||
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user