import {Adsa, RSA} from '../types'; import {HashCtor} from '@tios/hash'; export function extend(rsa: RSA): RSA { // @ts-ignore rsa.id = rsa.id || 'RSA'; // @ts-ignore rsa.algo = 'RSA'; if (!rsa.derive) { rsa.derive = () => { throw new Error('Unsupported') }; } rsa.adsa = (hash: HashCtor) => new RSAAdsa(rsa, hash); return rsa; } class RSAAdsa implements Adsa { readonly id: string; constructor(public asym: RSA, protected hash: HashCtor) { this.id = 'RSA_' + hash.id; } sign(msg: string | Buffer, key: any): Buffer { msg = Buffer.isBuffer(msg) ? msg : Buffer.from(msg); return this.asym.sign(this.hash, this.hash.digest(msg), key); } verify(msg: string | Buffer, sig: Buffer, key: any): boolean { msg = Buffer.isBuffer(msg) ? msg : Buffer.from(msg); return this.asym.verify(this.hash, this.hash.digest(msg), sig, key); } }