test_crypto_hash.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * Created by bmf on 10/31/13.
  3. */
  4. "use strict";
  5. var assert = require('assert');
  6. var sodium = require('../build/Release/sodium');
  7. var crypto = require('crypto');
  8. describe('Hash', function() {
  9. it('should return sha hash', function(done) {
  10. var buf = Buffer.alloc(100, 1);
  11. var r = sodium.crypto_hash(buf);
  12. var hashString = r.toString('hex');
  13. assert.equal(hashString, "ceacfdb0944ac37da84556adaac97bbc9a0190ae8ca091576b91ca70e134d1067da2dd5cc311ef147b51adcfbfc2d4086560e7af1f580db8bdc961d5d7a1f127");
  14. assert.equal(hashString, crypto.createHash('sha512').update(buf).digest('hex'));
  15. done();
  16. });
  17. it('should calculate same hash as the crypto module', function(done) {
  18. var buf = Buffer.alloc(100, 1);
  19. var r = sodium.crypto_hash(buf);
  20. var hashString = r.toString('hex');
  21. assert.equal(hashString, crypto.createHash('sha512').update(buf).digest('hex'));
  22. done();
  23. });
  24. it('should return sha512', function(done) {
  25. var buf = Buffer.alloc(100, 1);
  26. var r = sodium.crypto_hash_sha512(buf);
  27. var hashString = r.toString('hex');
  28. assert.equal(hashString, "ceacfdb0944ac37da84556adaac97bbc9a0190ae8ca091576b91ca70e134d1067da2dd5cc311ef147b51adcfbfc2d4086560e7af1f580db8bdc961d5d7a1f127");
  29. done();
  30. });
  31. it('should calculate same hash as the crypto module', function(done) {
  32. var buf = Buffer.alloc(100, 1);
  33. var r = sodium.crypto_hash_sha256(buf);
  34. var hashString = r.toString('hex');
  35. assert.equal(hashString, crypto.createHash('sha256').update(buf).digest('hex'));
  36. done();
  37. });
  38. });
  39. describe('crypto_hash_sha512 verify parameters', function() {
  40. it('bad param 1', function(done) {
  41. assert.throws(function() {
  42. var r = sodium.crypto_hash_sha512("buf");
  43. });
  44. assert.throws(function() {
  45. var r = sodium.crypto_hash_sha512(1);
  46. });
  47. done();
  48. });
  49. });
  50. describe('crypto_hash_sha verify parameters', function() {
  51. it('bad param 1', function(done) {
  52. assert.throws(function() {
  53. var r = sodium.crypto_hash_sha("buf");
  54. });
  55. assert.throws(function() {
  56. var r = sodium.crypto_hash_sha(1);
  57. });
  58. done();
  59. });
  60. });
  61. describe('crypto_hash_sha256 verify parameters', function() {
  62. it('bad param 1', function(done) {
  63. assert.throws(function() {
  64. var r = sodium.crypto_hash_sha256("buf");
  65. });
  66. assert.throws(function() {
  67. var r = sodium.crypto_hash_sha256(1);
  68. });
  69. done();
  70. });
  71. });