index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. var test = require('tape');
  2. var fs = require('fs');
  3. var constants = require('constants');
  4. var parseKeys = require('parse-asn1');
  5. require('./nodeTests');
  6. var priv1024 = fs.readFileSync(__dirname + '/rsa.1024.priv');
  7. var rsa1024 = {
  8. private: fs.readFileSync(__dirname + '/rsa.1024.priv'),
  9. public: fs.readFileSync(__dirname + '/rsa.1024.pub')
  10. };
  11. var rsa1024priv = {
  12. private: fs.readFileSync(__dirname + '/rsa.1024.priv'),
  13. public: fs.readFileSync(__dirname + '/rsa.1024.priv')
  14. };
  15. var rsa1024 = {
  16. private: fs.readFileSync(__dirname + '/rsa.1024.priv'),
  17. public: fs.readFileSync(__dirname + '/rsa.1024.pub')
  18. };
  19. var rsa2028 = {
  20. private: fs.readFileSync(__dirname + '/rsa.2028.priv'),
  21. public: fs.readFileSync(__dirname + '/rsa.2028.pub')
  22. };
  23. var nonrsa1024 = {
  24. private: fs.readFileSync(__dirname + '/1024.priv'),
  25. public: fs.readFileSync(__dirname + '/1024.pub')
  26. };
  27. var nonrsa1024str = {
  28. private: fs.readFileSync(__dirname + '/1024.priv').toString(),
  29. public: fs.readFileSync(__dirname + '/1024.pub').toString()
  30. };
  31. var pass1024 = {
  32. private: {
  33. passphrase: 'fooo',
  34. key:fs.readFileSync(__dirname + '/pass.1024.priv')
  35. },
  36. public: fs.readFileSync(__dirname + '/pass.1024.pub')
  37. };
  38. var pass2028 = {
  39. private: {
  40. passphrase: 'password',
  41. key:fs.readFileSync(__dirname + '/rsa.pass.priv')
  42. },
  43. public: fs.readFileSync(__dirname + '/rsa.pass.pub')
  44. };
  45. var nodeCrypto = require('../');
  46. var myCrypto = require('../browser');
  47. function _testIt(keys, message, t) {
  48. var pub = keys.public;
  49. var priv = keys.private;
  50. t.test(message.toString(), function (t) {
  51. t.plan(8);
  52. var myEnc = myCrypto.publicEncrypt(pub, message);
  53. var nodeEnc = nodeCrypto.publicEncrypt(pub, message);
  54. t.equals(myCrypto.privateDecrypt(priv, myEnc).toString('hex'), message.toString('hex'), 'my decrypter my message');
  55. t.equals(myCrypto.privateDecrypt(priv, nodeEnc).toString('hex'), message.toString('hex'), 'my decrypter node\'s message');
  56. t.equals(nodeCrypto.privateDecrypt(priv, myEnc).toString('hex'), message.toString('hex'), 'node decrypter my message');
  57. t.equals(nodeCrypto.privateDecrypt(priv, nodeEnc).toString('hex'), message.toString('hex'), 'node decrypter node\'s message');
  58. myEnc = myCrypto.privateEncrypt(priv, message);
  59. nodeEnc = nodeCrypto.privateEncrypt(priv, message);
  60. t.equals(myCrypto.publicDecrypt(pub, myEnc).toString('hex'), message.toString('hex'), 'reverse methods my decrypter my message');
  61. t.equals(myCrypto.publicDecrypt(pub, nodeEnc).toString('hex'), message.toString('hex'), 'reverse methods my decrypter node\'s message');
  62. t.equals(nodeCrypto.publicDecrypt(pub, myEnc).toString('hex'), message.toString('hex'), 'reverse methods node decrypter my message');
  63. t.equals(nodeCrypto.publicDecrypt(pub, nodeEnc).toString('hex'), message.toString('hex'), 'reverse methods node decrypter node\'s message');
  64. });
  65. }
  66. function testIt(keys, message, t) {
  67. _testIt(keys, message, t);
  68. _testIt(paddingObject(keys, 1), Buffer.concat([message, new Buffer(' with RSA_PKCS1_PADDING')]), t);
  69. var parsedKey = parseKeys(keys.public);
  70. var k = parsedKey.modulus.byteLength();
  71. var zBuf = new Buffer(k);
  72. zBuf.fill(0);
  73. var msg = Buffer.concat([zBuf, message, new Buffer(' with no padding')]).slice(-k);
  74. _testIt(paddingObject(keys, 3), msg, t);
  75. }
  76. function paddingObject(keys, padding) {
  77. return {
  78. public: addPadding(keys.public, padding),
  79. private: addPadding(keys.private, padding)
  80. };
  81. }
  82. function addPadding(key, padding) {
  83. if (typeof key === 'string' || Buffer.isBuffer(key)) {
  84. return {
  85. key: key,
  86. padding: padding
  87. };
  88. }
  89. var out = {
  90. key: key.key,
  91. padding:padding
  92. };
  93. if ('passphrase' in key) {
  94. out.passphrase = key.passphrase;
  95. }
  96. return out;
  97. }
  98. function testRun(i) {
  99. test('run ' + i, function (t) {
  100. testIt(rsa1024priv, new Buffer('1024 2 private keys'), t);
  101. testIt(rsa1024, new Buffer('1024 keys'), t);
  102. testIt(rsa2028, new Buffer('2028 keys'), t);
  103. testIt(nonrsa1024, new Buffer('1024 keys non-rsa key'), t);
  104. testIt(pass1024, new Buffer('1024 keys and password'), t);
  105. testIt(nonrsa1024str, new Buffer('1024 keys non-rsa key as a string'), t);
  106. testIt(pass2028, new Buffer('2028 rsa key with variant passwords'), t);
  107. });
  108. }
  109. var i = 0;
  110. var num = 20;
  111. while (++i <= 20) {
  112. testRun(i);
  113. }