Running "node lib-dqx-test.mjs" will: 1. Generate a secret key and print it out. 2. Generate a public key and print it out. 3. Generate a second key pair and print out only the public key. 4. Hash the second public key with SHA-256. 5. Sign the hash with the secret key of the first key pair. 6. Verify the signature with the public key of the first key pair.
37 lines
1.5 KiB
JavaScript
37 lines
1.5 KiB
JavaScript
import { dilithiumGenKeyPair, dilithiumSign, dilithiumVerifySig } from './api.js';
|
|
import { randomBytes, createHash } from 'crypto';
|
|
|
|
async function testDilithium() {
|
|
try {
|
|
// Generate the first key pair
|
|
const { publicKey: publicKey1, secretKey: secretKey1 } = await dilithiumGenKeyPair({ randomBytes });
|
|
console.log("First Key Pair Generated.");
|
|
console.log("Secret Key 1:", secretKey1.toString('hex'));
|
|
console.log("Public Key 1:", publicKey1.toString('hex'));
|
|
|
|
// Generate the second key pair
|
|
const { publicKey: publicKey2 } = await dilithiumGenKeyPair({ randomBytes });
|
|
console.log("Second Key Pair Generated.");
|
|
console.log("Public Key 2:", publicKey2.toString('hex'));
|
|
|
|
// Hash the second public key using SHA-256
|
|
const hash = createHash('sha256');
|
|
hash.update(publicKey2);
|
|
const hashedPublicKey2 = hash.digest();
|
|
console.log("Hashed Public Key 2:", hashedPublicKey2.toString('hex'));
|
|
|
|
// Sign the hash with the first secret key
|
|
const signature = await dilithiumSign({ secretKey: secretKey1, challenge: hashedPublicKey2 });
|
|
console.log("Signature:", signature.toString('hex'));
|
|
|
|
// Verify the signature with the first public key
|
|
const isValid = await dilithiumVerifySig({ publicKey: publicKey1, challenge: hashedPublicKey2, signature });
|
|
console.log("Signature Validity:", isValid);
|
|
|
|
} catch (error) {
|
|
console.error("Error in testDilithium:", error);
|
|
}
|
|
}
|
|
|
|
testDilithium();
|