Use curve functions from native module

This commit is contained in:
Scott Nonnenberg 2019-08-19 15:26:45 -07:00
parent e29eee4583
commit d3d2b0ec52
20 changed files with 623 additions and 74127 deletions

View file

@ -1,42 +1,13 @@
import { randomBytes } from 'crypto';
const g = global as any;
// Because curve wrapper will populate this
g.Internal = {};
// Because curve wrapper uses 'Module' to get at curve-provided functionality
// tslint:disable-next-line
g.Module = require('../../js/curve/curve25519_compiled');
// tslint:disable-next-line
require('../../js/curve/curve25519_wrapper');
export type BinaryType = Uint8Array | Buffer;
interface CurveType {
keyPair: (
privateKey: BinaryType
) => {
pubKey: BinaryType;
privKey: BinaryType;
};
sign: (privateKey: BinaryType, message: BinaryType) => BinaryType;
verify: (
publicKey: BinaryType,
message: BinaryType,
signature: BinaryType
) => boolean;
}
const {
keyPair: internalKeyPair,
sign: internalSign,
verify: internalVerify,
} = g.Internal.curve25519 as CurveType;
import {
calculateSignature,
generateKeyPair,
verifySignature,
} from 'curve25519-n';
export function keyPair() {
const privateKey = randomBytes(32);
const { pubKey, privKey } = internalKeyPair(privateKey);
const { pubKey, privKey } = generateKeyPair(privateKey);
return {
publicKey: pubKey,
@ -44,16 +15,16 @@ export function keyPair() {
};
}
export function sign(privateKey: BinaryType, message: BinaryType) {
return internalSign(privateKey, message);
export function sign(privateKey: Buffer, message: Buffer): Buffer {
return calculateSignature(privateKey, message);
}
export function verify(
publicKey: BinaryType,
message: BinaryType,
signature: BinaryType
) {
const failed = internalVerify(publicKey, message, signature);
publicKey: Buffer,
message: Buffer,
signature: Buffer
): boolean {
const failed = verifySignature(publicKey, message, signature);
return !failed;
}