'use strict'; const crypto = require ('../../index'); const key_length = 512; // eslint-disable-next-line max-lines-per-function describe ('rsa', () => { it ('should create a keypair', async () => { const k = await crypto.generate_keypair (); expect (k.private_key) .toMatch (/^-----BEGIN PRIVATE KEY-----.+-----END PRIVATE KEY-----\n$/su); expect (k.public_key) // eslint-disable-next-line max-len .toMatch (/^-----BEGIN RSA PUBLIC KEY-----.+-----END RSA PUBLIC KEY-----\n$/su); }); it ('should throw on too small key size', async () => { await expectAsync (crypto.generate_keypair (16)) .toBeRejectedWithError ( /key size too small/ui ); }); it ('should encrypt and decrypt', async () => { const k = await crypto.generate_keypair (key_length); const data = 'foobar'; const enc = crypto.asym_encrypt (data, k.public_key); const dec = crypto.asym_decrypt (enc, k.private_key); expect (dec) .toEqual (data); expect (enc).not.toEqual (data); }); it ('should throw if encryption key is invalid', () => { const key = '-----BEGIN RSA PUBLIC KEY-----\n' + 'MEgCCQDGvKLaq1SB/BTzocR4ZqGNr8dz1ylxxUpDncCu0C/ayOGPnCilB0LGEdqK\n' + 'rORlsYpIaDvLv6x6k8iSg6A/TsybAgMBAAE=\n' + '-----END RSA PUBLIC KEY-----\n'; const data = 'foobar'; expect (() => crypto.asym_encrypt (data, key)) .toThrow (); }); it ('should throw on invalid decryption key', () => { const data = 'EHsGr2eSVqZKTX1U9Qj4crGRFkk299kmxhiiO3fyaIS' + 'olB9+UYDdqrwcf/INwNddW4AzjA2Kf4dYaXIRLZsU1Q=='; const key = '-----BEGIN PRIVATE KEY-----\n' + 'MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAxryi2qtUgfwU86HE\n' + 'eGahja/Hc9cpccVKQ53ArtXv2sjhj5wopQdCxhHaiqzkZbGKSGg7y7+sepPIkoOg\n' + 'P07MmwIDAQABAkBKIk3hoi24+07ZfwuqGibDksGlLarxHLZSOMOKsnBXfRRyGqMr\n' + '/Z+qtQ9VPRWHBzGHZ9rXAVKa8gnRirik+ez5AiEA/XL6tOy92Yvxm46ewswmZ7ab\n' + 'V1KvChsXziaRj5eLzacCIQDIvLBO8og5Ng4r7E/dOAYrvzOLFqlN5UCuCRZzuFpv\n' + '7QIgCnj5ywgNQDP8I8Vc4ge1fouZF56fBPfhn+8QDLLiX/kCIHewKd+otJiIJoMB\n' + '78yTLvq+klkINgKAAsTCHmT5MtMxAiEAkFE70ms8C73JvTkd0znq8H6fBJV0iZxQ\n' + 'qOXON8bzv8A=\n' + '-----END PRIVATE KEY-----\n'; expect (() => crypto.asym_decrypt (data, key)) .toThrow (); }); it ('should throw on invalid decryption data', () => { const data = 'EHsGr2eSkqZKTX1U9Qj4crGRFkk299kmxhiiO3fyaIS' + 'olB9+UYDdqrwcf/INwNddW4AzjA2Kf4dYaXIRLZsU1Q=='; const key = '-----BEGIN PRIVATE KEY-----\n' + 'MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAxryi2qtUgfwU86HE\n' + 'eGahja/Hc9cpccVKQ53ArtAv2sjhj5wopQdCxhHaiqzkZbGKSGg7y7+sepPIkoOg\n' + 'P07MmwIDAQABAkBKIk3hoi24+07ZfwuqGibDksGlLarxHLZSOMOKsnBXfRRyGqMr\n' + '/Z+qtQ9VPRWHBzGHZ9rXAVKa8gnRirik+ez5AiEA/XL6tOy92Yvxm46ewswmZ7ab\n' + 'V1KvChsXziaRj5eLzacCIQDIvLBO8og5Ng4r7E/dOAYrvzOLFqlN5UCuCRZzuFpv\n' + '7QIgCnj5ywgNQDP8I8Vc4ge1fouZF56fBPfhn+8QDLLiX/kCIHewKd+otJiIJoMB\n' + '78yTLvq+klkINgKAAsTCHmT5MtMxAiEAkFE70ms8C73JvTkd0znq8H6fBJV0iZxQ\n' + 'qOXON8bzv8A=\n' + '-----END PRIVATE KEY-----\n'; expect (() => crypto.asym_decrypt (data, key)) .toThrow (); }); it ('should create a signature and verify it', async () => { const data = 'foobar'; const k = await crypto.generate_keypair (key_length); const signature = crypto.asym_sign (data, k.private_key); expect (typeof signature) .toEqual ('string'); expect (signature) .toMatch (/^[a-f0-9]+$/ui); expect (crypto.asym_verify (data, k.public_key, signature)) .toBeTrue (); }); it ('should throw on invalid sign key', () => { const key = '-----BEGIN PRIVATE KEY-----\n' + 'MIIBUwIBADANBgkqhkiG9w0BAQEFAASCAT0wggE5AgEAAkEAl4RZveQ8IXHPZTUf\n' + 'djwgDxIB5444yFvhIRasdasdasdApz+FmsXxGCatnPUCIHO9P1EfxasdaIq/Lpng\n' + 'y2ZQh2IaDYxC7K7tvGZwSY/CEcGfAiAcNgsg5ZMIQOWxn2KbFM81ne3b5FzD3Ozh\n' + 'GxItcBfKuw==\n' + '-----END PRIVATE KEY-----\n'; const data = 'foobar'; expect (() => crypto.asym_sign (data, key)) .toThrow (); }); it ('should throw on invalid verify key', () => { const key = '-----BEGIN RSA PUBLIC KEY-----\n' + 'MEgCQQCXhFm95DwDcc9lNasdasdasdasdasdHhEqigZupsWUxX/3tKn9tvEKV/\n' + 'zjoENWN63aorN+O+5MxDZMnEk+z9AgMBAAE=\n' + '-----END RSA PUBLIC KEY-----\n'; const data = 'foobar'; const signature = '3433ef165d6be80430e1107be0d7183bee769dbb38ea7d2737' + '1429c853fcab78bf1d3256fc16ee93a38cfd4ae79a74748e59fe9e9a65400c720d' + 'adb2dbcc1fa3'; expect (() => crypto.asym_verify (data, key, signature)) .toThrow (); }); it ('should not throw on invalid data or signature', () => { const key = '-----BEGIN RSA PUBLIC KEY-----\n' + 'MEgCQQC+9nw80cG+AsZ2euIZx4ptmUykJgEUgs4JiEvC+IaiIRAd9zGc0TcQAeND\n' + '171lw77kE02KJ4ARl1hkcLCW2bIrAgMBAAE=\n' + '-----END RSA PUBLIC KEY-----\n'; const data = 'foobar'; const signature = '6bae51b0449d71ca2e04099268bd0f6506a5dfc6f810bd72d' + '47865574a99910e404e5856da650cd45ee88365a2511fcc0866a0b5d1faf15c067' + 'ab8a4427554bf'; expect (crypto.asym_verify (data, key, signature)) .toBeTrue (); expect (crypto.asym_verify (data.replace ('b', 'c'), key, signature)) .toBeFalse (); expect (crypto.asym_verify (data, key, signature.replace ('f', 'e'))) .toBeFalse (); }); });