mirror of
https://github.com/tcsenpai/falconTS.git
synced 2025-06-01 09:10:09 +00:00
66 lines
2.8 KiB
TypeScript
66 lines
2.8 KiB
TypeScript
import { Falcon } from './src';
|
|
|
|
async function main() {
|
|
console.log("=== Falcon Cryptographic Operations ===");
|
|
|
|
// Create a new Falcon instance with default algorithm (falcon512_n3_v1)
|
|
const falcon = new Falcon();
|
|
await falcon.init();
|
|
console.log("Falcon initialized with algorithm:", await falcon.getAlgid());
|
|
|
|
// Generate a new key pair
|
|
console.log("\n=== Generating new keypair ===");
|
|
await falcon.genkey();
|
|
|
|
// Get the public and private keys in hex format
|
|
const publicKeyHex = await falcon.getPublicKeyHex();
|
|
const privateKeyHex = await falcon.getPrivateKeyHex();
|
|
|
|
// Get the public and private keys in base64 format (more compact)
|
|
const publicKeyBase64 = await falcon.getPublicKeyBase64();
|
|
const privateKeyBase64 = await falcon.getPrivateKeyBase64();
|
|
|
|
console.log("Public Key (hex):", publicKeyHex);
|
|
console.log("Public Key (base64):", publicKeyBase64);
|
|
console.log("Private Key (hex):", privateKeyHex);
|
|
console.log("Private Key (base64):", privateKeyBase64);
|
|
console.log("Size reduction:", Math.round((1 - publicKeyBase64.length / publicKeyHex.length) * 100) + '%');
|
|
|
|
// Sign a message
|
|
console.log("\n=== Signing a message ===");
|
|
const message = 'Hello, Falcon!';
|
|
const signatureHex = await falcon.signHex(message);
|
|
const signatureBase64 = await falcon.signBase64(message);
|
|
|
|
console.log("Message:", message);
|
|
console.log("Signature (hex):", signatureHex);
|
|
console.log("Signature (base64):", signatureBase64);
|
|
console.log("Size reduction:", Math.round((1 - signatureBase64.length / signatureHex.length) * 100) + '%');
|
|
|
|
// Verify the signature
|
|
console.log("\n=== Verifying signatures ===");
|
|
const isValidHex = await falcon.verifyHex(message, signatureHex);
|
|
const isValidBase64 = await falcon.verifyBase64(message, signatureBase64);
|
|
|
|
console.log("Verification (hex):", isValidHex);
|
|
console.log("Verification (base64):", isValidBase64);
|
|
|
|
// Create a new Falcon instance and import the private key
|
|
console.log("\n=== Importing private key ===");
|
|
const falcon2 = new Falcon();
|
|
await falcon2.init();
|
|
|
|
// Import using hex format
|
|
const derivedPublicKeyHex = await falcon2.setPrivateKeyHex(privateKeyHex);
|
|
console.log("Derived Public Key (hex):", derivedPublicKeyHex);
|
|
console.log("Keys match (hex):", derivedPublicKeyHex === publicKeyHex);
|
|
|
|
// Import using base64 format
|
|
const falcon3 = new Falcon();
|
|
await falcon3.init();
|
|
const derivedPublicKeyBase64 = await falcon3.setPrivateKeyBase64(privateKeyBase64);
|
|
console.log("Derived Public Key (base64):", derivedPublicKeyBase64);
|
|
console.log("Keys match (base64):", derivedPublicKeyBase64 === publicKeyBase64);
|
|
}
|
|
|
|
main().catch(console.error); |