💯 RSA tools. Lightweight. Zero Dependencies. Great tests. Universal compatibility.
rsa
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

test.sh 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. echo "defaults"
  114. node bin/rasha.js > /dev/null
  115. echo "jwk"
  116. node bin/rasha.js jwk > /dev/null
  117. echo "json 2048"
  118. node bin/rasha.js json 2048 > /dev/null
  119. echo "der"
  120. node bin/rasha.js der > /dev/null
  121. echo "pkcs8 der"
  122. node bin/rasha.js pkcs8 der > /dev/null
  123. echo "pem"
  124. node bin/rasha.js pem > /dev/null
  125. echo "pkcs1"
  126. node bin/rasha.js pkcs1 pem > /dev/null
  127. echo "spki"
  128. node bin/rasha.js spki > /dev/null
  129. echo "PASS"
  130. echo ""
  131. echo ""
  132. echo "Re-running tests with random keys of varying sizes"
  133. echo ""
  134. # commented out sizes below 512, since they are below minimum size on some systems.
  135. # rndkey 32 # minimum key size
  136. # rndkey 64
  137. # rndkey 128
  138. # rndkey 256
  139. rndkey 512
  140. rndkey 768
  141. rndkey 1024
  142. rndkey 2048 # first secure key size
  143. if [ "${RASHA_TEST_LARGE_KEYS}" == "true" ]; then
  144. rndkey 3072
  145. rndkey 4096 # largest reasonable key size
  146. else
  147. echo ""
  148. echo "Note:"
  149. echo "Keys larger than 2048 have been tested and work, but are omitted from automated tests to save time."
  150. echo "Set RASHA_TEST_LARGE_KEYS=true to enable testing of keys up to 4096."
  151. fi
  152. echo ""
  153. echo "Pass"
  154. rm fixtures/*.1.*
  155. echo ""
  156. echo "Testing Thumbprints"
  157. node bin/rasha.js ./fixtures/privkey-rsa-2048.pkcs1.pem thumbprint
  158. node bin/rasha.js ./fixtures/pub-rsa-2048.jwk.json thumbprint
  159. echo "PASS"
  160. echo ""
  161. echo ""
  162. echo "PASSED:"
  163. echo "• All inputs produced valid outputs"
  164. echo "• All outputs matched known-good values"
  165. echo "• All random tests passed reciprosity"