[Pkg-javascript-commits] [node-keygrip] 56/68: initial encrypt/decrypt methods

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 427f18803c38bf13a260fc0e467f363ad2f280bf
Author: Jonathan Ong <jonathanrichardong at gmail.com>
Date:   Sat May 17 15:58:33 2014 -0700

    initial encrypt/decrypt methods
---
 index.js | 58 +++++++++++++++++++++++++++++++++++++++++++++++-----------
 test.js  | 16 ++++++++++++++++
 2 files changed, 63 insertions(+), 11 deletions(-)

diff --git a/index.js b/index.js
index 995fb16..1ab7840 100644
--- a/index.js
+++ b/index.js
@@ -47,6 +47,42 @@ Keygrip.prototype = {
   encoding: 'base64',
 }
 
+// encrypt a message
+Keygrip.prototype.encrypt = function Keygrip$_encrypt(data, key, encoding) {
+  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)
+}
+
+// decrypt a single message
+// returns false on bad decrypts
+Keygrip.prototype._decrypt = function Keygrip$__decrypt(data, key, encoding) {
+  try {
+    var cipher = crypto.createDecipher(this.cipher, key)
+    cipher.update(data, this.encoding)
+    return cipher.final(encoding)
+  } 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]
@@ -58,17 +94,6 @@ Keygrip.prototype.sign = function Keygrip$_sign(data, key) {
     .replace(/\/|\+|=/g, _sign_replace)
 }
 
-// replace base64 characters with more friendly ones
-var _sign_replacements = {
-  "/": "_",
-  "+": "-",
-  "=": ""
-}
-
-function _sign_replace(x) {
-  return _sign_replacements[x]
-}
-
 Keygrip.prototype.verify = function Keygrip$_verify(data, digest) {
   return this.index(data, digest) > -1
 }
@@ -85,3 +110,14 @@ Keygrip.prototype.index = function Keygrip$_index(data, digest) {
 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 49db10b..7298664 100644
--- a/test.js
+++ b/test.js
@@ -38,6 +38,22 @@ describe('keygrip([key])', function () {
     hash = keys.sign("bieberschnitzel")
     assert.ok(/^[\w\-]{27}$/.test(hash))
   })
+
+  it('should encrypt a message', function () {
+    hash = keys.encrypt('lol')
+    assert.equal('lol', keys.decrypt(hash))
+  })
+
+  it('should return false on bad decryptions', function () {
+    var keys2 = new Keygrip(['lkjasdf'])
+    assert.equal(false, keys2.decrypt(hash))
+  })
+
+  it('should throw on bad inputs', function () {
+    assert.throws(function () {
+      keys.decrypt(hash + 'asdf')
+    })
+  })
 })
 
 describe('keygrip([keys...])', function () {

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