index.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. var crypto = require("crypto")
  2. function Keygrip(keys, algorithm, encoding) {
  3. if (!algorithm) algorithm = "sha1";
  4. if (!encoding) encoding = "base64";
  5. if (!(this instanceof Keygrip)) return new Keygrip(keys, algorithm, encoding)
  6. if (!keys || !(0 in keys)) {
  7. throw new Error("Keys must be provided.")
  8. }
  9. function sign(data, key) {
  10. return crypto
  11. .createHmac(algorithm, key)
  12. .update(data).digest(encoding)
  13. .replace(/\/|\+|=/g, function(x) {
  14. return ({ "/": "_", "+": "-", "=": "" })[x]
  15. })
  16. }
  17. this.sign = function(data){ return sign(data, keys[0]) }
  18. this.verify = function(data, digest) {
  19. return this.index(data, digest) > -1
  20. }
  21. this.index = function(data, digest) {
  22. for (var i = 0, l = keys.length; i < l; i++) {
  23. if (constantTimeCompare(digest, sign(data, keys[i]))) return i
  24. }
  25. return -1
  26. }
  27. }
  28. Keygrip.sign = Keygrip.verify = Keygrip.index = function() {
  29. throw new Error("Usage: require('keygrip')(<array-of-keys>)")
  30. }
  31. //http://codahale.com/a-lesson-in-timing-attacks/
  32. var constantTimeCompare = function(val1, val2){
  33. if(val1 == null && val2 != null){
  34. return false;
  35. } else if(val2 == null && val1 != null){
  36. return false;
  37. } else if(val1 == null && val2 == null){
  38. return true;
  39. }
  40. if(val1.length !== val2.length){
  41. return false;
  42. }
  43. var matches = 1;
  44. for(var i = 0; i < val1.length; i++){
  45. matches &= (val1.charAt(i) === val2.charAt(i) ? 1 : 0); //Don't short circuit
  46. }
  47. return matches === 1;
  48. };
  49. module.exports = Keygrip