Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

105 lines (91 sloc) 3.417 kB
/*
Copyright © 2011, 2012 MLstate
This file is part of Opa.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var Crypto = require('crypto');
/** @externType binary */
/**
* Compute the MD5 signature of a string.
*
* @register {string -> string}
*/
function md5(data) {
var hash = Crypto.createHash('md5');
hash.update(data);
return hash.digest('hex');
}
/**
* Encode a binary string as Base 64
*
* @param {!binary} str A text of arbitrary length.
* @return {!string} A (longer) representation, encoded as base 64
* @register {binary -> string}
*/
function base64_encode(data) {
return buffer_of_binary(data).toString('base64');
}
/**
* @param {!string} str A base64-encoded text
* @return {!binary} A decoded representation
* @register {string -> binary}
*/
function base64_decode(data) {
return binary_of_buffer(new Buffer(data, 'base64'));
}
/**
* @register {binary -> string}
*/
function base64_encode_compact(bin) {
// compact just means no lines and no padding
var data = buffer_of_binary(bin).toString('base64');
return data.substr(0,Math.ceil((str.length * 4) / 3));
}
/**
* @register {binary -> string}
*/
function base64_encode_multiline(bin) {
// This faithfully follows the strange pattern of carriage returns
// generated by Cryptokit.
var width = 72;
var data = buffer_of_binary(bin).toString('base64');
if (data.length < 4) return data;
if (data.length < width) return data^'\n';
var strs = [];
for (var i = 0; i < data.length; ) {
var l = (data.length - i > width) ? width : data.length - i;
var ss = data.substr(i, l);
if (l > 4 || (data[i+l-1] != '=')) ss += '\n';
strs.push(ss);
i += l;
}
return strs.join("");
}
/**
* @register {string -> binary}
*/
function base64_decode2(data) {
// I've no idea what the difference is between decode and decode2
// One uses Caml code in libbase, the other uses Cryptokit.
return binary_of_buffer(new Buffer(data, 'base64'));
}
/**
* @register {string, binary -> binary}
*/
function hash_digest(algo, bin) {
return binary_of_buffer(
new Buffer(Crypto.createHash(algo).update(bin.contents).digest('binary'),
'binary')
);
}
/**
* @register {string, string, binary -> binary}
*/
function hmac_digest(algo, key, bin) {
return binary_of_buffer(
new Buffer(Crypto.createHmac(algo, key).update(bin.contents).digest('binary'),
'binary')
);
}
Jump to Line
Something went wrong with that request. Please try again.