[Pkg-javascript-commits] [node-keygrip] 33/68: Parameterizing hmac algorithm and encoding

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Fri Jun 27 22:13:26 UTC 2014


This is an automated email from the git hooks/post-receive script.

andrewrk-guest pushed a commit to branch master
in repository node-keygrip.

commit d88e840fe93d9a92404a912163e3a8ea74c8b8bc
Author: fresheneesz <bitetrudpublic at gmail.com>
Date:   Sun Jul 14 12:33:34 2013 -0700

    Parameterizing hmac algorithm and encoding
---
 index.js | 10 ++++++----
 test.js  | 59 +++++++++++++++++++++++++++++++++++++----------------------
 2 files changed, 43 insertions(+), 26 deletions(-)

diff --git a/index.js b/index.js
index ce7ebcf..b7a1ef9 100644
--- a/index.js
+++ b/index.js
@@ -9,8 +9,10 @@ var crypto = require("crypto")
       ? JSON.parse(fs.readFileSync(keysPath))
       : undefined
 
-function Keygrip(keys) {
-  if (!(this instanceof Keygrip)) return new Keygrip(keys)
+function Keygrip(keys, algorithm, encoding) {
+  if (!algorithm) algorithm = "sha1";
+  if (!encoding) encoding = "base64";
+  if (!(this instanceof Keygrip)) return new Keygrip(keys, algorithm, encoding)
 
   if (!keys || !(0 in keys)) {
     if (keys = defaults) console.warn("No keys specified, using defaults instead.")
@@ -20,8 +22,8 @@ function Keygrip(keys) {
 
   function sign(data, key) {
     return crypto
-      .createHmac("sha1", key)
-      .update(data).digest("base64")
+      .createHmac(algorithm, key)
+      .update(data).digest(encoding)
       .replace(/\/|\+|=/g, function(x) {
         return ({ "/": "_", "+": "-", "=": "" })[x]
       })
diff --git a/test.js b/test.js
index 01ae674..6e3afb6 100644
--- a/test.js
+++ b/test.js
@@ -1,3 +1,5 @@
+"use strict";
+
 // ./test.js
 var assert = require("assert")
   , Keygrip = require("./")
@@ -14,28 +16,41 @@ keys = new Keygrip(/* empty list */)
 hash = keys.sign("bieberschnitzel")
 assert.ok(/^[\w\-]{27}$/.test(hash))
 
+
 // but we're going to use our list.
 // (note that the 'new' operator is optional)
-keylist = ["SEKRIT3", "SEKRIT2", "SEKRIT1"]
+keylist = ["SEKRIT3", "SEKRIT2", "SEKRIT1"] // keylist will be modified in place, so don't reuse
 keys = Keygrip(keylist)
-hash = keys.sign("bieberschnitzel")
-
-// .index returns the index of the first matching key
-index = keys.index("bieberschnitzel", hash)
-assert.equal(index, 0)
-
-// .verify returns the a boolean indicating a matched key
-matched = keys.verify("bieberschnitzel", hash)
-assert.ok(matched)
-
-index = keys.index("bieberschnitzel", "o_O")
-assert.equal(index, -1)
-
-// rotate a new key in, and an old key out
-keylist.unshift("SEKRIT4")
-keylist.pop()
-
-// if index > 0, it's time to re-sign
-index = keys.index("bieberschnitzel", hash)
-assert.equal(index, 1)
-hash = keys.sign("bieberschnitzel")
+testKeygripInstance(keys);
+
+
+// now pass in a different hmac algorithm and encoding
+keylist = ["Newest", "AnotherKey", "Oldest"]
+keys = Keygrip(keylist, "sha256", "hex")
+testKeygripInstance(keys);
+
+
+
+function testKeygripInstance(keys) {
+	hash = keys.sign("bieberschnitzel")
+	
+	// .index returns the index of the first matching key
+	index = keys.index("bieberschnitzel", hash)
+	assert.equal(index, 0)
+	
+	// .verify returns the a boolean indicating a matched key
+	var matched = keys.verify("bieberschnitzel", hash)
+	assert.ok(matched)
+	
+	index = keys.index("bieberschnitzel", "o_O")
+	assert.equal(index, -1)
+	
+	// rotate a new key in, and an old key out
+	keylist.unshift("SEKRIT4")
+	keylist.pop()
+	
+	// if index > 0, it's time to re-sign
+	index = keys.index("bieberschnitzel", hash)
+	assert.equal(index, 1)
+	hash = keys.sign("bieberschnitzel")	
+}
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-keygrip.git



More information about the Pkg-javascript-commits mailing list