💯 RSA tools. Lightweight. Zero Dependencies. Great tests. Universal compatibility.
rsa

test.sh 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/bin/bash
  2. # cause errors to hard-fail
  3. # (and diff non-0 exit status will cause failure)
  4. set -e
  5. pemtojwk() {
  6. keyid=$1
  7. if [ -z "$keyid" ]; then
  8. echo ""
  9. echo "Testing PEM-to-JWK PKCS#1"
  10. fi
  11. #
  12. node bin/rasha.js ./fixtures/privkey-rsa-2048.pkcs1.${keyid}pem \
  13. > ./fixtures/privkey-rsa-2048.jwk.1.json
  14. diff ./fixtures/privkey-rsa-2048.jwk.${keyid}json ./fixtures/privkey-rsa-2048.jwk.1.json
  15. #
  16. node bin/rasha.js ./fixtures/pub-rsa-2048.pkcs1.${keyid}pem \
  17. > ./fixtures/pub-rsa-2048.jwk.1.json
  18. diff ./fixtures/pub-rsa-2048.jwk.${keyid}json ./fixtures/pub-rsa-2048.jwk.1.json
  19. if [ -z "$keyid" ]; then
  20. echo "Pass"
  21. fi
  22. if [ -z "$keyid" ]; then
  23. echo ""
  24. echo "Testing PEM-to-JWK PKCS#8"
  25. fi
  26. #
  27. node bin/rasha.js ./fixtures/privkey-rsa-2048.pkcs8.${keyid}pem \
  28. > ./fixtures/privkey-rsa-2048.jwk.1.json
  29. diff ./fixtures/privkey-rsa-2048.jwk.${keyid}json ./fixtures/privkey-rsa-2048.jwk.1.json
  30. #
  31. node bin/rasha.js ./fixtures/pub-rsa-2048.spki.${keyid}pem \
  32. > ./fixtures/pub-rsa-2048.jwk.1.json
  33. diff ./fixtures/pub-rsa-2048.jwk.${keyid}json ./fixtures/pub-rsa-2048.jwk.1.json
  34. if [ -z "$keyid" ]; then
  35. echo "Pass"
  36. fi
  37. }
  38. jwktopem() {
  39. keyid=$1
  40. if [ -z "$keyid" ]; then
  41. echo ""
  42. echo "Testing JWK-to-PEM PKCS#1"
  43. fi
  44. #
  45. node bin/rasha.js ./fixtures/privkey-rsa-2048.jwk.${keyid}json pkcs1 \
  46. > ./fixtures/privkey-rsa-2048.pkcs1.1.pem
  47. diff ./fixtures/privkey-rsa-2048.pkcs1.${keyid}pem ./fixtures/privkey-rsa-2048.pkcs1.1.pem
  48. #
  49. node bin/rasha.js ./fixtures/pub-rsa-2048.jwk.${keyid}json pkcs1 \
  50. > ./fixtures/pub-rsa-2048.pkcs1.1.pem
  51. diff ./fixtures/pub-rsa-2048.pkcs1.${keyid}pem ./fixtures/pub-rsa-2048.pkcs1.1.pem
  52. if [ -z "$keyid" ]; then
  53. echo "Pass"
  54. fi
  55. if [ -z "$keyid" ]; then
  56. echo ""
  57. echo "Testing JWK-to-PEM PKCS#8"
  58. fi
  59. #
  60. node bin/rasha.js ./fixtures/privkey-rsa-2048.jwk.${keyid}json pkcs8 \
  61. > ./fixtures/privkey-rsa-2048.pkcs8.1.pem
  62. diff ./fixtures/privkey-rsa-2048.pkcs8.${keyid}pem ./fixtures/privkey-rsa-2048.pkcs8.1.pem
  63. #
  64. node bin/rasha.js ./fixtures/pub-rsa-2048.jwk.${keyid}json spki \
  65. > ./fixtures/pub-rsa-2048.spki.1.pem
  66. diff ./fixtures/pub-rsa-2048.spki.${keyid}pem ./fixtures/pub-rsa-2048.spki.1.pem
  67. if [ -z "$keyid" ]; then
  68. echo "Pass"
  69. fi
  70. if [ -z "$keyid" ]; then
  71. echo ""
  72. echo "Testing JWK-to-SSH"
  73. fi
  74. #
  75. node bin/rasha.js ./fixtures/privkey-rsa-2048.jwk.${keyid}json ssh > ./fixtures/pub-rsa-2048.ssh.1.pub
  76. diff ./fixtures/pub-rsa-2048.ssh.${keyid}pub ./fixtures/pub-rsa-2048.ssh.1.pub
  77. #
  78. node bin/rasha.js ./fixtures/pub-rsa-2048.jwk.${keyid}json ssh > ./fixtures/pub-rsa-2048.ssh.1.pub
  79. diff ./fixtures/pub-rsa-2048.ssh.${keyid}pub ./fixtures/pub-rsa-2048.ssh.1.pub
  80. if [ -z "$keyid" ]; then
  81. echo "Pass"
  82. fi
  83. }
  84. rndkey() {
  85. keyid="rnd.1."
  86. keysize=$1
  87. # Generate 2048-bit RSA Keypair
  88. openssl genrsa -out fixtures/privkey-rsa-2048.pkcs1.${keyid}pem $keysize
  89. # Convert PKCS1 (traditional) RSA Keypair to PKCS8 format
  90. openssl rsa -in fixtures/privkey-rsa-2048.pkcs1.${keyid}pem -pubout \
  91. -out fixtures/pub-rsa-2048.spki.${keyid}pem
  92. # Export Public-only RSA Key in PKCS1 (traditional) format
  93. openssl pkcs8 -topk8 -nocrypt -in fixtures/privkey-rsa-2048.pkcs1.${keyid}pem \
  94. -out fixtures/privkey-rsa-2048.pkcs8.${keyid}pem
  95. # Convert PKCS1 (traditional) RSA Public Key to SPKI/PKIX format
  96. openssl rsa -in fixtures/pub-rsa-2048.spki.${keyid}pem -pubin -RSAPublicKey_out \
  97. -out fixtures/pub-rsa-2048.pkcs1.${keyid}pem
  98. # Convert RSA public key to SSH format
  99. sshpub=$(ssh-keygen -f fixtures/pub-rsa-2048.spki.${keyid}pem -i -mPKCS8)
  100. echo "$sshpub rsa@localhost" > fixtures/pub-rsa-2048.ssh.${keyid}pub
  101. # to JWK
  102. node bin/rasha.js ./fixtures/privkey-rsa-2048.pkcs1.${keyid}pem \
  103. > ./fixtures/privkey-rsa-2048.jwk.${keyid}json
  104. node bin/rasha.js ./fixtures/pub-rsa-2048.pkcs1.${keyid}pem \
  105. > ./fixtures/pub-rsa-2048.jwk.${keyid}json
  106. pemtojwk "$keyid"
  107. jwktopem "$keyid"
  108. }
  109. pemtojwk ""
  110. jwktopem ""
  111. echo ""
  112. echo "testing node key generation"
  113. node bin/rasha.js > /dev/null
  114. node bin/rasha.js jwk > /dev/null
  115. node bin/rasha.js json 2048 > /dev/null
  116. node bin/rasha.js der > /dev/null
  117. node bin/rasha.js pkcs8 der > /dev/null
  118. node bin/rasha.js pem > /dev/null
  119. node bin/rasha.js pkcs1 pem > /dev/null
  120. node bin/rasha.js spki > /dev/null
  121. echo "PASS"
  122. echo ""
  123. echo ""
  124. echo "Re-running tests with random keys of varying sizes"
  125. echo ""
  126. # commented out sizes below 512, since they are below minimum size on some systems.
  127. # rndkey 32 # minimum key size
  128. # rndkey 64
  129. # rndkey 128
  130. # rndkey 256
  131. rndkey 512
  132. rndkey 768
  133. rndkey 1024
  134. rndkey 2048 # first secure key size
  135. if [ "${RASHA_TEST_LARGE_KEYS}" == "true" ]; then
  136. rndkey 3072
  137. rndkey 4096 # largest reasonable key size
  138. else
  139. echo ""
  140. echo "Note:"
  141. echo "Keys larger than 2048 have been tested and work, but are omitted from automated tests to save time."
  142. echo "Set RASHA_TEST_LARGE_KEYS=true to enable testing of keys up to 4096."
  143. fi
  144. echo ""
  145. echo "Pass"
  146. rm fixtures/*.1.*
  147. echo ""
  148. echo "Testing Thumbprints"
  149. node bin/rasha.js ./fixtures/privkey-rsa-2048.pkcs1.pem thumbprint
  150. node bin/rasha.js ./fixtures/pub-rsa-2048.jwk.json thumbprint
  151. echo "PASS"
  152. echo ""
  153. echo ""
  154. echo "PASSED:"
  155. echo "• All inputs produced valid outputs"
  156. echo "• All outputs matched known-good values"
  157. echo "• All random tests passed reciprosity"