signal-desktop/ts/updater/curve.ts
2020-09-21 15:42:51 -04:00

30 lines
652 B
TypeScript

import { randomBytes } from 'crypto';
import {
calculateSignature,
generateKeyPair,
verifySignature,
} from 'curve25519-n';
export function keyPair(): Record<string, Buffer> {
const privateKey = randomBytes(32);
const { pubKey, privKey } = generateKeyPair(privateKey);
return {
publicKey: pubKey,
privateKey: privKey,
};
}
export function sign(privateKey: Buffer, message: Buffer): Buffer {
return calculateSignature(privateKey, message);
}
export function verify(
publicKey: Buffer,
message: Buffer,
signature: Buffer
): boolean {
const failed = verifySignature(publicKey, message, signature);
return !failed;
}