test_sodium.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**
  2. *
  3. * @name node-sodium
  4. * @author bmf
  5. * @date 11/11/13
  6. * @version $
  7. */
  8. var assert = require('assert');
  9. var sodium = require('../lib/sodium');
  10. describe("Box", function () {
  11. it("Example of sending an encrypted message ", function (done) {
  12. // Generate random keys for alice and bob
  13. var alice = new sodium.Key.Box();
  14. var bob = new sodium.Key.Box();
  15. // Create a signature Alice's side using her secret key and Bob's public key
  16. var aliceBox = new sodium.Box(bob.pk(), alice.sk());
  17. var cipherText = aliceBox.encrypt("super secret message",
  18. "utf8");
  19. // Alice sends message to Bob, and he decrypts it
  20. var bobBox = new sodium.Box(alice.pk(), bob.sk());
  21. var plainText = aliceBox.decrypt(cipherText, "utf8");
  22. assert.equal(plainText, 'super secret message');
  23. done();
  24. });
  25. });
  26. describe("Sign", function () {
  27. it("Example of signing a message ", function (done) {
  28. // Alice's side
  29. // Generate random signing keys for alice
  30. var aliceKey = new sodium.Key.Sign();
  31. // Alice signs the message
  32. var aliceSign = new sodium.Sign(aliceKey);
  33. var signature = aliceSign.sign("important signed message",
  34. "utf8");
  35. // Alice sends signed message to bob
  36. // Bob's side
  37. // Bob receives signature and tries to verify it.
  38. // As the var signature includes Alice's public key Bob can simply call verify
  39. // Please note that in this simple form Bob can only be sure that the message
  40. // was signed with the secret key that is paired with the public key inside
  41. // signature. Bob cannot be certain that the public key is indeed Alice's public key
  42. // Using public keys directly, ie, without a digital certificate is open to
  43. // impersonation using 'man-in-the-middle' attacks.
  44. var bobMsg = new sodium.Sign.verify(signature);
  45. assert.ok(bobMsg);
  46. // Bob checks the message extracted from the signature
  47. assert.equal(bobMsg.toString('utf8'), 'important signed message');
  48. done();
  49. });
  50. });
  51. describe("Auth", function () {
  52. it("Using authentication tokens ", function (done) {
  53. /**
  54. * User Side
  55. *
  56. * The user will use its key ID and secret key to authenticate a message that she
  57. * is sending to the server.
  58. * The Key ID allows the server to later retrieve the secret key from its database
  59. */
  60. var restKeyID = '123123';
  61. var restAPIKey =
  62. 'afcd09812fe556aac311de3faade13afcd09812fe556aac311de3faade13ade3';
  63. // User's request to the server
  64. var request = 'http://api.example.domain/query?keyid=' +
  65. restKeyID;
  66. // Create authentication object
  67. var auth = new sodium.Auth(restAPIKey);
  68. // Generate authentication token for the request
  69. var authToken = auth.generate(request, 'utf8');
  70. // Append the authToken to the request
  71. var token = authToken.toString('base64');
  72. request += "&token=" + token;
  73. // Send request to server.
  74. /**
  75. * Server Side
  76. *
  77. * HTTP Server processes the query string and extracts the token and key Id
  78. * To keep the example simple we just reuse the token, restAPIKey and restKeyId
  79. * variables.
  80. *
  81. * After extracting the token and key ID from the query string the server can
  82. * retrieve the corresponding secret key from a database, and use it to validate
  83. * the authentication token.
  84. */
  85. var serverAuth = new sodium.Auth(restAPIKey);
  86. // In this simulation we just recreate the original query before token was appended
  87. request = 'http://api.example.domain/query?keyid=' + restKeyID;
  88. // Convert the token to a buffer
  89. var tBuffer = sodium.Utils.toBuffer(token, 'base64');
  90. /**
  91. * If validate returns true then the token could only have been generated by
  92. * the user that has the same secret key as identified by key Id.
  93. * The keys and key Ids are usually generated by an administrator when the user
  94. * signs up for the service.
  95. */
  96. assert.ok(serverAuth.validate(tBuffer, request, 'utf8'));
  97. done();
  98. });
  99. });
  100. describe("OneTimeAuth", function () {
  101. it("Using one time authentication tokens ", function (done) {
  102. /**
  103. * User Side
  104. *
  105. * The user will use its key ID and secret key to authenticate a message that she
  106. * is sending to the server.
  107. * The Key ID allows the server to later retrieve the secret key from its database
  108. *
  109. * As the name implies this authentication scheme should only be used one time,
  110. * for one message, using the same key. So you should change keys after each
  111. * message.
  112. */
  113. var restKeyID = '123123';
  114. var restAPIKey =
  115. 'afcd09812fe556aac311de3faade13afcd09812fe556aac311de3faade13ade3';
  116. // User's request to the server
  117. var request = 'http://api.example.domain/query?keyid=' +
  118. restKeyID;
  119. // Create authentication object
  120. var auth = new sodium.OneTimeAuth(restAPIKey);
  121. // Generate authentication token for the request
  122. var authToken = auth.generate(request, 'utf8');
  123. // Append the authToken to the request
  124. var token = authToken.toString('base64');
  125. request += "&token=" + token;
  126. // Send request to server.
  127. /**
  128. * Server Side
  129. *
  130. * HTTP Server processes the query string and extracts the token and key Id
  131. * To keep the example simple we just reuse the token, restAPIKey and restKeyId
  132. * variables.
  133. *
  134. * After extracting the token and key ID from the query string the server can
  135. * retrieve the corresponding secret key from a database, and use it to validate
  136. * the authentication token.
  137. */
  138. var serverAuth = new sodium.OneTimeAuth(restAPIKey);
  139. // In this simulation we just recreate the original query before token was appended
  140. request = 'http://api.example.domain/query?keyid=' + restKeyID;
  141. // Convert the token to a buffer
  142. var tBuffer = sodium.Utils.toBuffer(token, 'base64');
  143. /**
  144. * If validate returns true then the token could only have been generated by
  145. * the user that has the same secret key as identified by key Id.
  146. * The keys and key Ids are usually generated by an administrator when the user
  147. * signs up for the service.
  148. */
  149. assert.ok(serverAuth.validate(tBuffer, request, 'utf8'));
  150. done();
  151. });
  152. });
  153. describe("SecretBox", function () {
  154. it("Example of sending an encrypted message ", function (done) {
  155. // Alice and Bob both agree on a secret key that they have
  156. // shared in some secure way
  157. // Alice's Side
  158. var aliceKey = Buffer.from("fcd09812fe556aac311de3faade13afa");
  159. var aliceBox = new sodium.SecretBox(aliceKey);
  160. // Create a signature Alice's side using her secret key and Bob's public key
  161. var cipherText = aliceBox.encrypt("super secret message",
  162. "utf8");
  163. // Alice sends message to Bob, and he decrypts it
  164. // Bob's Side
  165. // Bob uses the same scret key as Alice
  166. var bobKey = Buffer.from("fcd09812fe556aac311de3faade13afa");
  167. var bobBox = new sodium.SecretBox(bobKey);
  168. var plainText = bobBox.decrypt(cipherText, "utf8");
  169. assert.equal(plainText, 'super secret message');
  170. done();
  171. });
  172. });
  173. describe("ECDH", function () {
  174. it("should calculate the same secret", function (done) {
  175. var bob = new sodium.Key.ECDH();
  176. var alice = new sodium.Key.ECDH();
  177. var aliceDH = new sodium.ECDH(bob.pk(), alice.sk());
  178. var bobDH = new sodium.ECDH(alice.pk(), bob.sk());
  179. var bobSecret = bobDH.secret();
  180. var aliceSecret = aliceDH.secret();
  181. assert.deepEqual(bobSecret, aliceSecret);
  182. done();
  183. });
  184. it("should calculate the same session key", function (done) {
  185. var bob = new sodium.Key.ECDH();
  186. var alice = new sodium.Key.ECDH();
  187. var aliceDH = new sodium.ECDH(bob.pk().get(), alice.sk().get());
  188. var bobDH = new sodium.ECDH(alice.pk().get(), bob.sk().get());
  189. var bobSecret = bobDH.sessionKey();
  190. var aliceSecret = aliceDH.sessionKey();
  191. assert.deepEqual(bobSecret, aliceSecret);
  192. done();
  193. });
  194. });