var crypto = require('crypto');

exports.md5 = function (str){
	var md5sum = crypto.createHash('md5');
	md5sum.update(str);
	str = md5sum.digest('hex');
	return str;
};
 
exports.sha256 = function(str) {
	var sha = crypto.createHash('sha256');
	sha.update(str);
	return sha.digest('hex');
}

exports.sha1 = function (str){
	var sha1sum = crypto.createHash('sha1');
	sha1sum.update(str);
	str = sha1sum.digest('hex');
	return str;
};

exports.url_encode = function (url){
	url = encodeURIComponent(url);
	url = url.replace(/\%3A/g, ":");
	url = url.replace(/\%2F/g, "/");
	url = url.replace(/\%3F/g, "?");
	url = url.replace(/\%3D/g, "=");
	url = url.replace(/\%26/g, "&");

	return url;
}

exports.hmac_sha1 = function (app_key,str){
	crypto.createHmac('sha1', app_key).update(str).digest().toString('base64'); 
}

// 创建加密算法
exports.aseEncode = function(data, password) {
	const cipher = crypto.createCipher('aes192', password);
	let crypted = cipher.update(data, 'utf-8', 'hex');
	crypted += cipher.final('hex');
	return crypted;
};

// 创建解密算法
exports.aseDecode = function(data, password) {
	const decipher = crypto.createDecipher('aes192', password);
	let decrypted = decipher.update(data, 'hex', 'utf-8');
	decrypted += decipher.final('utf-8');
	return decrypted;
};