标签:
/* hash.js */
var crypto = require(‘crypto‘);
module.exports = function(){
this.encode = function(){
var algorithm = arguments[0] ? arguments[0] : ‘‘
, enstring = arguments[1] ? arguments[1] : ‘‘
, returnType = arguments[2] ? arguments[2] : ‘‘;
if( algorithm ){
var hash = crypto.createHash(algorithm);
hash.update(enstring);
return hash.digest(returnType);
}
console.log(‘Please input encryption param‘);
}
}
/* target.js */
module.exports = function(){
this.encode = function(){}
this.decode = function(){}
}
/* adapter.js */
var util = require(‘util‘),
Target = require(‘./target‘);
function Adapter(){
Target.call(this);
this.encode = function(){
var encodeModule = arguments[0] ? arguments[0] : null
, algorithm = arguments[1] ? arguments[1] : ‘‘
, enstring = arguments[2] ? arguments[2] : ‘‘
, returnType = arguments[3] ? arguments[3] : ‘‘
,AdapteeClass = require("./" + encodeModule)
,adapteeObj = new AdapteeClass();
return adapteeObj.encode(algorithm, enstring, returnType, encodeKey, encodeType);
}
}
util.inherits(Adapter,Target);
module.exports = Adapter;
/* test.js */
var AdapterClass = require(‘./adapter‘);
var Adapter = new AdapterClass();
var hashEncodeStr = Adapter.encode(‘hash‘, ‘md5‘, ‘yuejide‘, ‘hex‘);
console.log(hashEncodeStr);
var http = require(‘http‘);
var crypto = require(‘crypto‘)
http.createServer(function(req,res){
res.writeHead(200, {‘Content-Type‘ : ‘text/html‘});
var md5 = crypto.createHash(‘md5‘);
var passwd = md5.update(‘admin‘).digest(‘hex‘);
res.end(passwd);
}).listen(8888);
crypto加密
标签:
原文地址:http://www.cnblogs.com/gide/p/4448891.html