Verified Commit d48ec577 authored by Samuel Elliott's avatar Samuel Elliott
Browse files

Fix signcryption keypair types

parent 4ba2a4d6
...@@ -196,7 +196,7 @@ import * as tweetnacl from 'tweetnacl'; ...@@ -196,7 +196,7 @@ import * as tweetnacl from 'tweetnacl';
// If you know the sender's public key you can pass it to DearmorAndVerifyStream and it will emit an error if it doesn't match // If you know the sender's public key you can pass it to DearmorAndVerifyStream and it will emit an error if it doesn't match
const sender_key: Uint8Array = tweetnacl.sign.keyPair().publicKey; const sender_key: Uint8Array = tweetnacl.sign.keyPair().publicKey;
const stream = new DearmorAndVerifyStream(recipient_keypair, sender_key); const stream = new DearmorAndVerifyStream(sender_key);
stream.on('end', () => { stream.on('end', () => {
// If you didn't pass the sender's public key you should check it now // If you didn't pass the sender's public key you should check it now
......
...@@ -19,7 +19,7 @@ export let debug_fix_key: Buffer | null = null; ...@@ -19,7 +19,7 @@ export let debug_fix_key: Buffer | null = null;
export let debug_fix_keypair: tweetnacl.BoxKeyPair | null = null; export let debug_fix_keypair: tweetnacl.BoxKeyPair | null = null;
export async function signcrypt( export async function signcrypt(
data: Uint8Array | string, keypair: tweetnacl.BoxKeyPair | null, recipients_keys: Uint8Array[] data: Uint8Array | string, keypair: tweetnacl.SignKeyPair | null, recipients_keys: Uint8Array[]
): Promise<Buffer> { ): Promise<Buffer> {
const chunks = chunkBuffer(data, CHUNK_LENGTH); const chunks = chunkBuffer(data, CHUNK_LENGTH);
...@@ -58,13 +58,13 @@ export async function signcrypt( ...@@ -58,13 +58,13 @@ export async function signcrypt(
export class SigncryptStream extends Transform { export class SigncryptStream extends Transform {
readonly payload_key: Buffer; readonly payload_key: Buffer;
readonly ephemeral_keypair: tweetnacl.BoxKeyPair; readonly ephemeral_keypair: tweetnacl.BoxKeyPair;
readonly keypair: tweetnacl.BoxKeyPair | null; readonly keypair: tweetnacl.SignKeyPair | null;
readonly header: SigncryptedMessageHeader; readonly header: SigncryptedMessageHeader;
private in_buffer = Buffer.alloc(0); private in_buffer = Buffer.alloc(0);
private payload_index = BigInt(0); private payload_index = BigInt(0);
private i = 0; private i = 0;
constructor(keypair: tweetnacl.BoxKeyPair | null, recipients_keys: Uint8Array[]) { constructor(keypair: tweetnacl.SignKeyPair | null, recipients_keys: Uint8Array[]) {
super(); super();
// 1. Generate a random 32-byte payload key. // 1. Generate a random 32-byte payload key.
......
...@@ -66,7 +66,7 @@ export async function signAndArmor(data: Uint8Array | string, keypair: tweetnacl ...@@ -66,7 +66,7 @@ export async function signAndArmor(data: Uint8Array | string, keypair: tweetnacl
const signed = sign(data, keypair); const signed = sign(data, keypair);
return armor(signed, {message_type: MessageType.SIGNED_MESSAGE}); return armor(signed, {message_type: MessageType.SIGNED_MESSAGE});
} }
export async function verifyArmored(signed: string, public_key: Uint8Array): Promise<DearmorAndVerifyResult> { export async function verifyArmored(signed: string, public_key?: Uint8Array | null): Promise<DearmorAndVerifyResult> {
const dearmored = dearmor(signed); const dearmored = dearmor(signed);
return Object.assign(await verify(dearmored, public_key), { return Object.assign(await verify(dearmored, public_key), {
remaining: dearmored.remaining, remaining: dearmored.remaining,
...@@ -113,9 +113,9 @@ export async function signDetachedAndArmor(data: Uint8Array | string, keypair: t ...@@ -113,9 +113,9 @@ export async function signDetachedAndArmor(data: Uint8Array | string, keypair: t
return armor(signed, {message_type: MessageType.DETACHED_SIGNATURE}); return armor(signed, {message_type: MessageType.DETACHED_SIGNATURE});
} }
export async function verifyDetachedArmored( export async function verifyDetachedArmored(
signed: string, data: Uint8Array | string, public_key: Uint8Array signature: string, data: Uint8Array | string, public_key?: Uint8Array | null
): Promise<DearmorAndVerifyDetachedResult> { ): Promise<DearmorAndVerifyDetachedResult> {
const dearmored = dearmor(signed); const dearmored = dearmor(signature);
const result = await verifyDetached(dearmored, data, public_key); const result = await verifyDetached(dearmored, data, public_key);
return { return {
...@@ -131,16 +131,16 @@ export interface DearmorAndVerifyDetachedResult extends VerifyDetachedResult { ...@@ -131,16 +131,16 @@ export interface DearmorAndVerifyDetachedResult extends VerifyDetachedResult {
} }
export async function signcryptAndArmor( export async function signcryptAndArmor(
data: Uint8Array | string, keypair: tweetnacl.BoxKeyPair | null, recipients_keys: Uint8Array[] data: Uint8Array | string, keypair: tweetnacl.SignKeyPair | null, recipients_keys: Uint8Array[]
) { ) {
const encrypted = await signcrypt(data, keypair, recipients_keys); const encrypted = await signcrypt(data, keypair, recipients_keys);
return armor(encrypted, {message_type: MessageType.ENCRYPTED_MESSAGE}); return armor(encrypted, {message_type: MessageType.ENCRYPTED_MESSAGE});
} }
export async function dearmorAndDesigncrypt( export async function dearmorAndDesigncrypt(
encrypted: string, keypair: tweetnacl.BoxKeyPair signcrypted: string, keypair: tweetnacl.BoxKeyPair, sender?: Uint8Array | null
): Promise<DearmorAndDesigncryptResult> { ): Promise<DearmorAndDesigncryptResult> {
const dearmored = dearmor(encrypted); const dearmored = dearmor(signcrypted);
return Object.assign(await designcrypt(dearmored, keypair), { return Object.assign(await designcrypt(dearmored, keypair, sender), {
remaining: dearmored.remaining, remaining: dearmored.remaining,
header_info: dearmored.header_info, header_info: dearmored.header_info,
}); });
...@@ -150,7 +150,7 @@ export type DearmorAndDesigncryptResult = DearmorResult & DesigncryptResult; ...@@ -150,7 +150,7 @@ export type DearmorAndDesigncryptResult = DearmorResult & DesigncryptResult;
export class SigncryptAndArmorStream extends Pumpify { export class SigncryptAndArmorStream extends Pumpify {
constructor( constructor(
keypair: tweetnacl.BoxKeyPair | null, recipients_keys: Uint8Array[], armor_options?: Partial<ArmorOptions> keypair: tweetnacl.SignKeyPair | null, recipients_keys: Uint8Array[], armor_options?: Partial<ArmorOptions>
) { ) {
const encrypt = new SigncryptStream(keypair, recipients_keys); const encrypt = new SigncryptStream(keypair, recipients_keys);
const armor = new ArmorStream(Object.assign({ const armor = new ArmorStream(Object.assign({
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment