var softdog = require("softdog");
var input = {
type: "password",
value: "abc731"
}
// 客戶端加密
var client2serverStr = softdog.encrypt(input.value);
console.log(client2serverStr);
// 服務器端解密
var rawStr = softdog.decrypt(client2serverStr);
console.log(rawStr);
// 結果校驗
console.log(rawStr === input.value);
複製代碼
加密|解密方法參數若是數據類型非字符串或者爲空字符串,返回結果均爲空字符串javascript
softdogjava
/***
* @module {Object} softdog 簡單的字符串加密|解密,防止密碼提交攔截
* @interface Softdog
* @method encrypt ---------- 字符串加密
* @method decrypt ---------- 字符串解密
*/
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
// 功能函數
function _confuse(str, sign) {
if (typeof str !== 'string' || !str) {
return '';
}
var newStr = '';
for (var i = 0; i < str.length; i++) {
newStr += String.fromCharCode(str.charCodeAt(i) + sign * 2);
}
return newStr;
}
;
var softdog = {
encrypt: function (str) {
return _confuse(str, 1);
},
decrypt: function (str) {
return _confuse(str, -1);
}
};
return softdog;
});
複製代碼