Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

index.js 4.1KB

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