react / wstein / node_modules / browserify / node_modules / crypto-browserify / node_modules / public-encrypt / test / nodeTests.js
80551 viewsvar crypto = require('../browser');1var test = require('tape');2var fs = require('fs');34// Test RSA encryption/decryption5test('node tests', function (t) {6var certPem = fs.readFileSync(__dirname + '/test_cert.pem', 'ascii');7var keyPem = fs.readFileSync(__dirname + '/test_key.pem', 'ascii');8var rsaPubPem = fs.readFileSync(__dirname + '/test_rsa_pubkey.pem',9'ascii');10var rsaKeyPem = fs.readFileSync(__dirname + '/test_rsa_privkey.pem',11'ascii');12var rsaKeyPemEncrypted = fs.readFileSync(13__dirname + '/test_rsa_privkey_encrypted.pem', 'ascii');14var input = 'I AM THE WALRUS';15var bufferToEncrypt = new Buffer(input);1617var encryptedBuffer = crypto.publicEncrypt(rsaPubPem, bufferToEncrypt);1819var decryptedBuffer = crypto.privateDecrypt(rsaKeyPem, encryptedBuffer);20t.equal(input, decryptedBuffer.toString());2122var decryptedBufferWithPassword = crypto.privateDecrypt({23key: rsaKeyPemEncrypted,24passphrase: 'password'25}, encryptedBuffer);26t.equal(input, decryptedBufferWithPassword.toString());2728// encryptedBuffer = crypto.publicEncrypt(certPem, bufferToEncrypt);2930// decryptedBuffer = crypto.privateDecrypt(keyPem, encryptedBuffer);31// t.equal(input, decryptedBuffer.toString());3233encryptedBuffer = crypto.publicEncrypt(keyPem, bufferToEncrypt);3435decryptedBuffer = crypto.privateDecrypt(keyPem, encryptedBuffer);36t.equal(input, decryptedBuffer.toString());3738encryptedBuffer = crypto.privateEncrypt(keyPem, bufferToEncrypt);3940decryptedBuffer = crypto.publicDecrypt(keyPem, encryptedBuffer);41t.equal(input, decryptedBuffer.toString());4243t.throws(function() {44crypto.privateDecrypt({45key: rsaKeyPemEncrypted,46passphrase: 'wrong'47}, encryptedBuffer);48});49t.end();50});5152