You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
2.2 KiB

4 years ago
  1. var mods = [
  2. 'secp256k1',
  3. 'secp224r1',
  4. 'prime256v1',
  5. 'prime192v1'
  6. ]
  7. var test = require('tape')
  8. var _crypto = require('../')
  9. var createECDH1 = _crypto.createECDH
  10. var createECDH2 = require('create-ecdh/browser')
  11. mods.forEach(function (mod) {
  12. test('createECDH: ' + mod + ' uncompressed', function (t) {
  13. t.plan(2)
  14. var dh1 = createECDH1(mod)
  15. dh1.generateKeys()
  16. var dh2 = createECDH2(mod)
  17. dh2.generateKeys()
  18. var pubk1 = dh1.getPublicKey()
  19. var pubk2 = dh2.getPublicKey()
  20. t.notEquals(pubk1.toString('hex'), pubk2.toString('hex'), 'diff public keys')
  21. var pub1 = dh1.computeSecret(pubk2).toString('hex')
  22. var pub2 = dh2.computeSecret(pubk1).toString('hex')
  23. t.equals(pub1, pub2, 'equal secrets')
  24. })
  25. test('createECDH: ' + mod + ' compressed', function (t) {
  26. t.plan(2)
  27. var dh1 = createECDH1(mod)
  28. dh1.generateKeys()
  29. var dh2 = createECDH2(mod)
  30. dh2.generateKeys()
  31. var pubk1 = dh1.getPublicKey(null, 'compressed')
  32. var pubk2 = dh2.getPublicKey(null, 'compressed')
  33. t.notEquals(pubk1.toString('hex'), pubk2.toString('hex'), 'diff public keys')
  34. var pub1 = dh1.computeSecret(pubk2).toString('hex')
  35. var pub2 = dh2.computeSecret(pubk1).toString('hex')
  36. t.equals(pub1, pub2, 'equal secrets')
  37. })
  38. test('createECDH: ' + mod + ' set stuff', function (t) {
  39. t.plan(5)
  40. var dh1 = createECDH1(mod)
  41. var dh2 = createECDH2(mod)
  42. dh1.generateKeys()
  43. dh2.generateKeys()
  44. dh1.setPrivateKey(dh2.getPrivateKey())
  45. dh1.setPublicKey(dh2.getPublicKey())
  46. var priv1 = dh1.getPrivateKey('hex')
  47. var priv2 = dh2.getPrivateKey('hex')
  48. t.equals(priv1, priv2, 'same private key')
  49. var pubk1 = dh1.getPublicKey()
  50. var pubk2 = dh2.getPublicKey()
  51. t.equals(pubk1.toString('hex'), pubk2.toString('hex'), 'same public keys, uncompressed')
  52. t.equals(dh1.getPublicKey('hex', 'compressed'), dh2.getPublicKey('hex', 'compressed'), 'same public keys compressed')
  53. t.equals(dh1.getPublicKey('hex', 'hybrid'), dh2.getPublicKey('hex', 'hybrid'), 'same public keys hybrid')
  54. var pub1 = dh1.computeSecret(pubk2).toString('hex')
  55. var pub2 = dh2.computeSecret(pubk1).toString('hex')
  56. t.equals(pub1, pub2, 'equal secrets')
  57. })
  58. })