feat: init
This commit is contained in:
51
node_modules/keygrip/index.js
generated
vendored
Normal file
51
node_modules/keygrip/index.js
generated
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
/*!
|
||||
* keygrip
|
||||
* Copyright(c) 2011-2014 Jed Schmidt
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
'use strict'
|
||||
|
||||
var compare = require('tsscmp')
|
||||
var crypto = require("crypto")
|
||||
|
||||
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)) {
|
||||
throw new Error("Keys must be provided.")
|
||||
}
|
||||
|
||||
function sign(data, key) {
|
||||
return crypto
|
||||
.createHmac(algorithm, key)
|
||||
.update(data).digest(encoding)
|
||||
.replace(/\/|\+|=/g, function(x) {
|
||||
return ({ "/": "_", "+": "-", "=": "" })[x]
|
||||
})
|
||||
}
|
||||
|
||||
this.sign = function(data){ return sign(data, keys[0]) }
|
||||
|
||||
this.verify = function(data, digest) {
|
||||
return this.index(data, digest) > -1
|
||||
}
|
||||
|
||||
this.index = function(data, digest) {
|
||||
for (var i = 0, l = keys.length; i < l; i++) {
|
||||
if (compare(digest, sign(data, keys[i]))) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
||||
}
|
||||
|
||||
Keygrip.sign = Keygrip.verify = Keygrip.index = function() {
|
||||
throw new Error("Usage: require('keygrip')(<array-of-keys>)")
|
||||
}
|
||||
|
||||
module.exports = Keygrip
|
||||
Reference in New Issue
Block a user