nodeTests.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. var crypto = require('../browser');
  2. var test = require('tape');
  3. var fs = require('fs');
  4. // Test RSA encryption/decryption
  5. test('node tests', function (t) {
  6. var certPem = fs.readFileSync(__dirname + '/test_cert.pem', 'ascii');
  7. var keyPem = fs.readFileSync(__dirname + '/test_key.pem', 'ascii');
  8. var rsaPubPem = fs.readFileSync(__dirname + '/test_rsa_pubkey.pem',
  9. 'ascii');
  10. var rsaKeyPem = fs.readFileSync(__dirname + '/test_rsa_privkey.pem',
  11. 'ascii');
  12. var rsaKeyPemEncrypted = fs.readFileSync(
  13. __dirname + '/test_rsa_privkey_encrypted.pem', 'ascii');
  14. var input = 'I AM THE WALRUS';
  15. var bufferToEncrypt = new Buffer(input);
  16. var encryptedBuffer = crypto.publicEncrypt(rsaPubPem, bufferToEncrypt);
  17. var decryptedBuffer = crypto.privateDecrypt(rsaKeyPem, encryptedBuffer);
  18. t.equal(input, decryptedBuffer.toString());
  19. var decryptedBufferWithPassword = crypto.privateDecrypt({
  20. key: rsaKeyPemEncrypted,
  21. passphrase: 'password'
  22. }, encryptedBuffer);
  23. t.equal(input, decryptedBufferWithPassword.toString());
  24. // encryptedBuffer = crypto.publicEncrypt(certPem, bufferToEncrypt);
  25. // decryptedBuffer = crypto.privateDecrypt(keyPem, encryptedBuffer);
  26. // t.equal(input, decryptedBuffer.toString());
  27. encryptedBuffer = crypto.publicEncrypt(keyPem, bufferToEncrypt);
  28. decryptedBuffer = crypto.privateDecrypt(keyPem, encryptedBuffer);
  29. t.equal(input, decryptedBuffer.toString());
  30. encryptedBuffer = crypto.privateEncrypt(keyPem, bufferToEncrypt);
  31. decryptedBuffer = crypto.publicDecrypt(keyPem, encryptedBuffer);
  32. t.equal(input, decryptedBuffer.toString());
  33. t.throws(function() {
  34. crypto.privateDecrypt({
  35. key: rsaKeyPemEncrypted,
  36. passphrase: 'wrong'
  37. }, encryptedBuffer);
  38. });
  39. t.end();
  40. });