[Pkg-javascript-commits] [node-keygrip] 58/68: initial 2.0.0 support

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Fri Jun 27 22:13:28 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 b254f77fb960229eb703e311f9d8fb235a626348
Author: Jonathan Ong <jonathanrichardong at gmail.com>
Date:   Tue Jun 3 22:27:52 2014 -0700

    initial 2.0.0 support
---
 History.md |  8 ++++++
 index.js   | 91 ++++++++++++++++++++++++++++++--------------------------------
 test.js    | 10 +++----
 3 files changed, 57 insertions(+), 52 deletions(-)

diff --git a/History.md b/History.md
new file mode 100644
index 0000000..945cf48
--- /dev/null
+++ b/History.md
@@ -0,0 +1,8 @@
+
+2.0.0 / 2014-06-
+==================
+
+ * remove the `[algorithm]` option, use `.hash=` instead
+ * remove the `[encoding]` option, `Buffer`s are now always returned
+ * add `.encrypt()`, `.decrypt()`, and `.cipher=`
+ * default hash algorithm is now `sha256`
diff --git a/index.js b/index.js
index 1ab7840..b9bc1c7 100644
--- a/index.js
+++ b/index.js
@@ -4,31 +4,35 @@ var constantTimeCompare = require('scmp')
 
 module.exports = Keygrip
 
-function Keygrip(keys, algorithm, encoding) {
+function Keygrip(keys) {
+  if (arguments.length > 1) {
+    console.warn('as of v2, keygrip() only accepts a single argument.')
+    console.warn('set keygrip().hash= instead.')
+    console.warn('keygrip() also now only supports buffers.')
+  }
+
   if (!Array.isArray(keys) || !keys.length) throw new Error("Keys must be provided.")
-  if (!(this instanceof Keygrip)) return new Keygrip(keys, algorithm, encoding)
+  if (!(this instanceof Keygrip)) return new Keygrip(keys)
 
   this.keys = keys
-  if (algorithm) this.algorithm = algorithm
-  if (encoding) this.encoding = encoding
 }
 
 /**
- * Allow setting `keygrip.algorithm = 'sha1'`
+ * Allow setting `keygrip.hash = 'sha1'`
  * or `keygrip.cipher = 'aes256'`
  * with validation instead of always doing `keygrip([], alg, enc)`.
  * This also allows for easier defaults.
  */
 
 Keygrip.prototype = {
-  get algorithm() {
-    return this._algorithm
+  get hash() {
+    return this._hash
   },
 
-  set algorithm(val) {
+  set hash(val) {
     if (!~crypto.getHashes().indexOf(val))
       throw new Error('unsupported hash algorithm: ' + val)
-    this._algorithm = val
+    this._hash = val
   },
 
   get cipher() {
@@ -42,56 +46,56 @@ Keygrip.prototype = {
   },
 
   // defaults
-  _algorithm: 'sha1',
+  _hash: 'sha256',
   _cipher: 'aes256',
-  encoding: 'base64',
 }
 
 // encrypt a message
-Keygrip.prototype.encrypt = function Keygrip$_encrypt(data, key, encoding) {
+Keygrip.prototype.encrypt = function Keygrip$_encrypt(data, iv, key) {
   key = key || this.keys[0]
 
-  var cipher = crypto.createCipher(this.cipher, key)
-  cipher.update(data, encoding)
-  return cipher
-    .final(this.encoding)
-    .replace(/\/|\+|=/g, _sign_replace)
+  var cipher = iv
+    ? crypto.createCipheriv(this.cipher, key, iv)
+    : crypto.createCipher(this.cipher, key)
+  cipher.update(data)
+  return cipher.final()
 }
 
 // decrypt a single message
 // returns false on bad decrypts
-Keygrip.prototype._decrypt = function Keygrip$__decrypt(data, key, encoding) {
+Keygrip.prototype.decrypt = function Keygrip$__decrypt(data, iv, key) {
+  if (!key) {
+    // decrypt every key
+    var keys = this.keys
+    for (var i = 0, l = keys.length; i < l; i++) {
+      var message = this.decrypt(data, iv, keys[i])
+      if (message !== false) return message
+    }
+
+    return false
+  }
+
   try {
-    var cipher = crypto.createDecipher(this.cipher, key)
-    cipher.update(data, this.encoding)
-    return cipher.final(encoding)
+    var cipher = iv
+      ? crypto.createDecipheriv(this.cipher, key, iv)
+      : crypto.createDecipher(this.cipher, key)
+    cipher.update(data)
+    return cipher.final()
   } catch (err) {
     if (/bad decrypt/.test(err.message)) return false
     throw err
   }
 }
 
-// decrypt every key
-Keygrip.prototype.decrypt = function Keygrip$_decrypt(data, encoding) {
-  var keys = this.keys
-  for (var i = 0, l = keys.length; i < l; i++) {
-    var message = this._decrypt(data, keys[i], encoding)
-    if (message !== false) return message
-  }
-
-  return false
-}
-
 // message signing
 Keygrip.prototype.sign = function Keygrip$_sign(data, key) {
   // default to the first key
   key = key || this.keys[0]
 
   return crypto
-    .createHmac(this.algorithm, key)
+    .createHmac(this.hash, key)
     .update(data)
-    .digest(this.encoding)
-    .replace(/\/|\+|=/g, _sign_replace)
+    .digest()
 }
 
 Keygrip.prototype.verify = function Keygrip$_verify(data, digest) {
@@ -107,17 +111,10 @@ Keygrip.prototype.index = function Keygrip$_index(data, digest) {
   return -1
 }
 
-Keygrip.sign = Keygrip.verify = Keygrip.index = function() {
+Keygrip.encrypt =
+Keygrip.decrypt =
+Keygrip.sign =
+Keygrip.verify =
+Keygrip.index = function() {
   throw new Error("Usage: require('keygrip')(<array-of-keys>)")
 }
-
-// replace base64 characters with more friendly ones
-var _sign_replacements = {
-  "/": "_",
-  "+": "-",
-  "=": ""
-}
-
-function _sign_replace(x) {
-  return _sign_replacements[x]
-}
diff --git a/test.js b/test.js
index 7298664..c0ad4f6 100644
--- a/test.js
+++ b/test.js
@@ -13,10 +13,10 @@ describe('keygrip(keys)', function () {
     }, /must be provided/);
   })
 
-  it('should throw when setting an invalid algorithm', function () {
+  it('should throw when setting an invalid hash algorithm', function () {
     var keys = new Keygrip(['a', 'b'])
     assert.throws(function () {
-      keys.algorithm = 'asdf'
+      keys.hash = 'asdf'
     }, /unsupported/)
   })
 
@@ -36,12 +36,12 @@ describe('keygrip([key])', function () {
     // .sign returns the hash for the first key
     // all hashes are SHA1 HMACs in url-safe base64
     hash = keys.sign("bieberschnitzel")
-    assert.ok(/^[\w\-]{27}$/.test(hash))
+    assert.ok(/^[\w\+=]{44}$/.test(hash.toString('base64')))
   })
 
   it('should encrypt a message', function () {
     hash = keys.encrypt('lol')
-    assert.equal('lol', keys.decrypt(hash))
+    assert.equal('lol', keys.decrypt(hash).toString('utf8'))
   })
 
   it('should return false on bad decryptions', function () {
@@ -68,7 +68,7 @@ describe('keygrip([keys...])', function () {
   it('should sign a string with a different algorithm and encoding', function () {
     // now pass in a different hmac algorithm and encoding
     keylist = ["Newest", "AnotherKey", "Oldest"]
-    keys = Keygrip(keylist, "sha256", "hex")
+    keys = Keygrip(keylist)
     testKeygripInstance(keys);
   })
 })

-- 
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