var crypto = require('crypto');

var md5 = exports.md5 = function (data, format) {
    format = format || 'hex';
    var hash = crypto.createHash('md5');
    hash.update(data);
    return hash.digest(format);
};

var sha1 = exports.sha1 = function (data, format) {
    format = format || 'hex';
    var hash = crypto.createHash('sha1');
    hash.update(data);
    return hash.digest(format);
};

var sha256 = exports.sha256 = function (data, format) {
    format = format || 'hex';
    var hash = crypto.createHash('sha256');
    hash.update(data);
    return hash.digest(format);
};

var convertHexToBase64 = exports.convertHexToBase64 = function (hex) {
    var bytesArr = [];
    for (var i = 0, len = hex.length - 1; i < len; i = i + 2) {
        var str = hex[i] + hex[i + 1];
        bytesArr.push(parseInt(str, 16));
    }

    return new Buffer(bytesArr).toString('base64');
};
