Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

nodeTests.js 1.7KB

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