UNPKG

braintree

Version:

A library for integrating with Braintree.

84 lines (67 loc) 2.1 kB
//@ sourceMappingURL=digest.map // Generated by CoffeeScript 1.6.1 var Digest, crypto, _; crypto = require('crypto'); _ = require('underscore'); Digest = (function() { function Digest() {} Digest.Sha1hexdigest = function(privateKey, string) { return new Digest().hmacSha1(privateKey, string); }; Digest.Sha256hexdigest = function(privateKey, string) { return new Digest().hmacSha256(privateKey, string); }; Digest.secureCompare = function(left, right) { return new Digest().secureCompare(left, right); }; Digest.prototype.hmacSha256 = function(key, data) { var hmac; hmac = crypto.createHmac('sha256', this.sha256(key)); hmac.update(data); return hmac.digest('hex'); }; Digest.prototype.hmacSha1 = function(key, data) { var hmac; hmac = crypto.createHmac('sha1', this.sha1(key)); hmac.update(data); return hmac.digest('hex'); }; Digest.prototype.secureCompare = function(left, right) { var left_byte, left_bytes, result, right_byte, right_bytes, _i, _len, _ref, _ref1; if (!((left != null) && (right != null))) { return false; } left_bytes = this.unpack(left); right_bytes = this.unpack(right); result = 0; _ref = _.zip(left_bytes, right_bytes); for (_i = 0, _len = _ref.length; _i < _len; _i++) { _ref1 = _ref[_i], left_byte = _ref1[0], right_byte = _ref1[1]; result |= left_byte ^ right_byte; } return result === 0; }; Digest.prototype.sha1 = function(data) { var hash; hash = crypto.createHash('sha1'); hash.update(data); return hash.digest(); }; Digest.prototype.sha256 = function(data) { var hash; hash = crypto.createHash('sha256'); hash.update(data); return hash.digest(); }; Digest.prototype.unpack = function(string) { var bytes, character, index, _i, _len; bytes = []; for (index = _i = 0, _len = string.length; _i < _len; index = ++_i) { character = string[index]; bytes.push(string.charCodeAt(index)); } return bytes; }; return Digest; })(); exports.Digest = Digest;