2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2019-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-03-28 17:09:26 +00:00
|
|
|
import { randomBytes } from 'crypto';
|
2019-08-19 22:26:45 +00:00
|
|
|
import {
|
|
|
|
calculateSignature,
|
|
|
|
generateKeyPair,
|
|
|
|
verifySignature,
|
|
|
|
} from 'curve25519-n';
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2020-09-16 19:31:05 +00:00
|
|
|
export function keyPair(): Record<string, Buffer> {
|
2019-03-28 17:09:26 +00:00
|
|
|
const privateKey = randomBytes(32);
|
2019-08-19 22:26:45 +00:00
|
|
|
const { pubKey, privKey } = generateKeyPair(privateKey);
|
2019-03-28 17:09:26 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
publicKey: pubKey,
|
|
|
|
privateKey: privKey,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-08-19 22:26:45 +00:00
|
|
|
export function sign(privateKey: Buffer, message: Buffer): Buffer {
|
|
|
|
return calculateSignature(privateKey, message);
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function verify(
|
2019-08-19 22:26:45 +00:00
|
|
|
publicKey: Buffer,
|
|
|
|
message: Buffer,
|
|
|
|
signature: Buffer
|
|
|
|
): boolean {
|
|
|
|
const failed = verifySignature(publicKey, message, signature);
|
2019-03-28 17:09:26 +00:00
|
|
|
|
|
|
|
return !failed;
|
|
|
|
}
|