具体实现方法如下,迷失网络

作者: 网络资讯  发布:2019-09-06

本文实例讲述了JavaScript实现SHA-1加密算法的方法。分享给大家供大家参考。具体实现方法如下:

JavaScript实现SHA-1加密算法的方法,javascriptsha-1

本文实例讲述了JavaScript实现SHA-1加密算法的方法。分享给大家供大家参考。具体实现方法如下:

调用方法:hex_sha1即可。
复制代码 代码如下:/*
 *
 * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
 * in FIPS PUB 180-1
 *
 * By lizq
 *
 * 2006-11-11
 *
 */
/*
 *
 * Configurable variables.
 *
 */
var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
/*
 *
 * The main function to calculate message digest
 *
 */
function hex_sha1(s){
    return binb2hex(core_sha1(AlignSHA1(s)));
}
/*
 *
 * Perform a simple self-test to see if the VM is working
 *
 */
function sha1_vm_test(){
    return hex_sha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d";
}
/*
 *
 * Calculate the SHA-1 of an array of big-endian words, and a bit length
 *
 */
function core_sha1(blockArray){
    var x = blockArray; // append padding
    var w = Array(80);
    var a = 1732584193;
    var b = -271733879;
    var c = -1732584194;
    var d = 271733878;
    var e = -1009589776;
    for (var i = 0; i < x.length; i = 16) // 每次处理512位 16*32
    {
        var olda = a;
        var oldb = b;
        var oldc = c;
        var oldd = d;
        var olde = e;
        for (var j = 0; j < 80; j ) // 对每个512位进行80步操作
        {
            if (j < 16)
                w[j] = x[i j];
            else
                w[j] = rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
            var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)), safe_add(safe_add(e, w[j]), sha1_kt(j)));
            e = d;
            d = c;
            c = rol(b, 30);
            b = a;
            a = t;
        }
        a = safe_add(a, olda);
        b = safe_add(b, oldb);
        c = safe_add(c, oldc);
        d = safe_add(d, oldd);
        e = safe_add(e, olde);
    }
    return new Array(a, b, c, d, e);
}
/*
 *
 * Perform the appropriate triplet combination function for the current
 * iteration
 *
 * 返回对应F函数的值
 *
 */
function sha1_ft(t, b, c, d){
    if (t < 20)
        return (b & c) | ((~ b) & d);
    if (t < 40)
        return b ^ c ^ d;
    if (t < 60)
        return (b & c) | (b & d) | (c & d);
    return b ^ c ^ d; // t<80
}
/*
 *
 * Determine the appropriate additive constant for the current iteration
 *
 * 返回对应的Kt值
 *
 */
function sha1_kt(t){
    return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 : (t < 60) ? -1894007588 : -899497514;
}
/*
 *
 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
 *
 * to work around bugs in some JS interpreters.
 *
 * 将32位数拆成高16位和低16位分别进行相加,从而实现 MOD 2^32 的加法
 *
 */
function safe_add(x, y){
    var lsw = (x & 0xFFFF) (y & 0xFFFF);
    var msw = (x >> 16) (y >> 16) (lsw >> 16);
    return (msw << 16) | (lsw & 0xFFFF);
}
/*
 *
 * Bitwise rotate a 32-bit number to the left.
 *
 * 32位二进制数循环左移
 *
 */
function rol(num, cnt){
    return (num << cnt) | (num >>> (32 - cnt));
}
/*
 *
 * The standard SHA1 needs the input string to fit into a block
 *
 * This function align the input string to meet the requirement
 *
 */
function AlignSHA1(str){
    var nblk = ((str.length 8) >> 6) 1, blks = new Array(nblk * 16);
    for (var i = 0; i < nblk * 16; i )
        blks[i] = 0;
    for (i = 0; i < str.length; i )
        blks[i >> 2] |= str.charCodeAt(i) << (24 - (i & 3) * 8);
    blks[i >> 2] |= 0x80 << (24 - (i & 3) * 8);
    blks[nblk * 16 - 1] = str.length * 8;
    return blks;
}
/*
 *
 * Convert an array of big-endian words to a hex string.
 *
 */
function binb2hex(binarray){
    var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
    var str = "";
    for (var i = 0; i < binarray.length * 4; i ) {
        str = hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8 4)) & 0xF)
        hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8)) & 0xF);
    }
    return str;
}
/*
 *
 * calculate MessageDigest accord to source message that inputted
 *
 */
function calcDigest(){
    var digestM = hex_sha1(document.SHAForm.SourceMessage.value);
    document.SHAForm.MessageDigest.value = digestM;
}

希望本文所述对大家的javascript程序设计有所帮助。

本文实例讲述了JavaScript实现SHA-1加密算法的方法。分享给大家供大家参考。具体实现方法如下...

sha1加密varsha = hex_sha1('mima123465')

/****************************************************************
    JScript 常用运行库    Version 1.11
                    
    作者    :    Lostinet[迷失网络]
    Email    :    lostinet@chongjian.com
****************************************************************/

调用方法:hex_sha1即可。

var sha = hex_sha1('mima123465')

function LostinetJScriptRuntimeLibrary(){}

复制代码 代码如下:

<script>

/****************************************************************
    Global
****************************************************************/
function Global(){}
function Global.GetUndefined(){}
function Global.ValueOf(unknown){
    if(Global.IsUndefined(unknown))return Global.GetUndefined();
    try{
        return unknown.valueOf();
    }catch(x){}
    return unknown;
}

/*
 *
 * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
 * in FIPS PUB 180-1
 *
 * By lizq
 *
 * 2006-11-11
 *
 */
/*
 *
 * Configurable variables.
 *
 */
var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
/*
 *
 * The main function to calculate message digest
 *
 */
function hex_sha1(s){
    return binb2hex(core_sha1(AlignSHA1(s)));
}
/*
 *
 * Perform a simple self-test to see if the VM is working
 *
 */
function sha1_vm_test(){
    return hex_sha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d";
}
/*
 *
 * Calculate the SHA-1 of an array of big-endian words, and a bit length
 *
 */
function core_sha1(blockArray){
    var x = blockArray; // append padding
    var w = Array(80);
    var a = 1732584193;
    var b = -271733879;
    var c = -1732584194;
    var d = 271733878;
    var e = -1009589776;
    for (var i = 0; i < x.length; i = 16) // 每次处理512位 16*32
    {
        var olda = a;
        var oldb = b;
        var oldc = c;
        var oldd = d;
        var olde = e;
        for (var j = 0; j < 80; j ) // 对每个512位进行80步操作
        {
            if (j < 16)
                w[j] = x[i j];
            else
                w[j] = rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
            var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)), safe_add(safe_add(e, w[j]), sha1_kt(j)));
            e = d;
            d = c;
            c = rol(b, 30);
            b = a;
            a = t;
        }
        a = safe_add(a, olda);
        b = safe_add(b, oldb);
        c = safe_add(c, oldc);
        d = safe_add(d, oldd);
        e = safe_add(e, olde);
    }
    return new Array(a, b, c, d, e);
}
/*
 *
 * Perform the appropriate triplet combination function for the current
 * iteration
 *
 * 返回对应F函数的值
 *
 */
function sha1_ft(t, b, c, d){
    if (t < 20)
        return (b & c) | ((~ b) & d);
    if (t < 40)
        return b ^ c ^ d;
    if (t < 60)
        return (b & c) | (b & d) | (c & d);
    return b ^ c ^ d; // t<80
}
/*
 *
 * Determine the appropriate additive constant for the current iteration
 *
 * 返回对应的Kt值
 *
 */
function sha1_kt(t){
    return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 : (t < 60) ? -1894007588 : -899497514;
}
/*
 *
 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
 *
 * to work around bugs in some JS interpreters.
 *
 * 将32位数拆成高16位和低16位分别进行相加,从而实现 MOD 2^32 的加法
 *
 */
function safe_add(x, y){
    var lsw = (x & 0xFFFF) (y & 0xFFFF);
    var msw = (x >> 16) (y >> 16) (lsw >> 16);
    return (msw << 16) | (lsw & 0xFFFF);
}
/*
 *
 * Bitwise rotate a 32-bit number to the left.
 *
 * 32位二进制数循环左移
 *
 */
function rol(num, cnt){
    return (num << cnt) | (num >>> (32 - cnt));
}
/*
 *
 * The standard SHA1 needs the input string to fit into a block
 *
 * This function align the input string to meet the requirement
 *
 */
function AlignSHA1(str){
    var nblk = ((str.length 8) >> 6) 1, blks = new Array(nblk * 16);
    for (var i = 0; i < nblk * 16; i )
        blks[i] = 0;
    for (i = 0; i < str.length; i )
        blks[i >> 2] |= str.charCodeAt(i) << (24 - (i & 3) * 8);
    blks[i >> 2] |= 0x80 << (24 - (i & 3) * 8);
    blks[nblk * 16 - 1] = str.length * 8;
    return blks;
}
/*
 *
 * Convert an array of big-endian words to a hex string.
 *
 */
function binb2hex(binarray){
    var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
    var str = "";
    for (var i = 0; i < binarray.length * 4; i ) {
        str = hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8 4)) & 0xF)
        hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8)) & 0xF);
    }
    return str;
}
/*
 *
 * calculate MessageDigest accord to source message that inputted
 *
 */
function calcDigest(){
    var digestM = hex_sha1(document.SHAForm.SourceMessage.value);
    document.SHAForm.MessageDigest.value = digestM;
}

      //sha1加密

function Global.ToString(unknown){
    if(Global.IsUndefined(unknown))return "";
    if(Global.IsJScriptObject(unknown))
    {
        try{
            return String.Convert(unknown.toString());
        }catch(x){}
    }
    return String.Convert(unknown);
}
function Global.IsBlank(unknown){
    switch(typeof(unknown))
    {
    case "undefined":return true;
    case "string":return unknown=="";
    case "number":return unknown==0;
    case "object":return unknown==null;
    case "function":return false;
    default:return unknown?false:true;
    }
}
function Global.IsJScriptType(unknown)
{
    switch(typeof(unknown))
    {
    case "object":
        return Global.IsJScriptObject(unknown);
    case "function":
    case "string":
    case "number":
    case "boolean":
    case "undefined":
        return true;
    default:
        return false;
    }
}
function Global.IsJScriptObject(unknown)
{
    if(typeof(unknown)=="function")return true;
    if(typeof(unknown)!="object")return false;
    try{
        return typeof(unknown.constructor)=="function";
    }catch(x){
    }
    return false;
}
function Global.ThrowError(msg){
    throw(new Error(-1,msg));
}
/****************************************************************
    Object
****************************************************************/

希望本文所述对大家的javascript程序设计有所帮助。

/*

/*
unknown,function,ActiveXObject等会被认为是undefined
除了Object和Array,其他object类型的子属性会被忽略
自定义的constructor会自动转为Object
*/
//需要优化
function Object.Encode(v)
{
    var Encoders={"unknown":_unknown,"object":_object,"function":_function,"string":_string,"number":_number,"boolean":_boolean,"undefined":_undefined};
    return InnerEncode(v);
    function InnerEncode(v)
    {
        var der=Encoders[typeof(v)];
        if(der)
            return der(v);
        return _undefined(v);
    }
    function EncodeString(v){
        v=String.Convert(v);
        v=v.replace(/&/g,"&0");
        v=v.replace(/=/g,"&1");
        v=v.replace(/:/g,"&2");
        v=v.replace(/,/g,"&3");
        return v;
    }
    function _unknown(v)
    {
        return _undefined(v);
    }
    function _object(v)
    {
        if(!Global.IsJScriptObject(v))
            return _undefined(v);
        switch(v.constructor)
        {
        case Array:        return _Array(v);
        case Date:        return _Date(v);
        case String:    return _String(v);
        case Number:    return _Number(v);
        case Boolean:    return _Boolean(v);
        case RegExp:    return _RegExp(v);
        case Object:
        default:
            return _Object(v);
        }
        function _Object(v)
        {
            var str="object_Object=";
            var arr=[];
            for(var i in v)
            {
                if(Global.IsJScriptType(v[i])&&typeof(v[i])!="function")
                    arr[arr.length]=EncodeString(EncodeString(i) ":" EncodeString(InnerEncode(v[i])));
            }
            return str arr.join(",");
        }
        function _Array(v)
        {
            var str="object_Array=";
            var arr=[];
            for(var i in v)
            {
                if(Global.IsJScriptType(v[i])&&typeof(v[i])!="function")
                    arr[arr.length]=EncodeString(EncodeString(i) ":" EncodeString(InnerEncode(v[i])));
            }
            return str arr.join(",");
        }
        function _Date(v){return "object_Date=" v.getTime();}
        function _String(v){return "object_String=" EncodeString(v);}
        function _Number(v){return "object_Number=" EncodeString(v);}
        function _Boolean(v){return "object_Boolean=" EncodeString(v.ToString());}
        function _RegExp(v)
        {
            return "object_RegExp="
                EncodeString(
                    "p:" EncodeString(v)
                    "i:" EncodeString(Boolean.ToString(v.ignoreCase))
                    "g:" EncodeString(Boolean.ToString(v.global))
                );
        }
    }
    function _function(v){return _undefined(v);}
    function _string(v){return "string=" EncodeString(v);}
    function _number(v){return "number=" EncodeString(v);}
    function _boolean(v){return "boolean=" EncodeString(v.ToString());}
    function _undefined(v){return "undefined=undefined";}
}
//需要优化
function Object.Decode(s)
{
    s=String.Convert(s);
    var Decoders={"object_Object":_object_Object,"object_Array":_object_Array,"object_Date":_object_Date,"object_String":_object_String,"object_Number":_object_Number,"object_Boolean":_object_Boolean,"object_RegExp":_object_RegExp,"string":_string,"number":_number,"boolean":_boolean,"undefined":_undefined};
    return InnerDecode(s);
    function InnerDecode(s)
    {
        s=String.Convert(s);
        var arr=s.split("=");
        var type=arr[0];
        var der=Decoders[type];
        if(der)
            return der(String.Convert(arr[1]));
        return _undefined(String.Convert(arr[1]));
    }
    function DecodeString(v){
        v=String.Convert(v);
        v=v.replace(/&3/g,",");
        v=v.replace(/&2/g,":");
        v=v.replace(/&1/g,"=");
        v=v.replace(/&0/g,"&");
        return v;
    }
    function _object_Object(ss)
    {
        var obj=new Object();
        var arr=ss.split(",");
        for(var i=0;i<arr.length;i )
        {
            var arr2=DecodeString(arr[i]).split(":");
            var index=DecodeString(arr2[0]);
            if(index=="")break;
            obj[index]=InnerDecode(DecodeString(arr2[1]));
        }
        return obj;
    }
    function _object_Array(ss)
    {
        var obj=new Array();
        var arr=ss.split(",");
        for(var i=0;i<arr.length;i )
        {
            var arr2=DecodeString(arr[i]).split(":");
            var index=DecodeString(arr2[0]);
            if(index=="")break;
            obj[index]=InnerDecode(DecodeString(arr2[1]));
        }
        return obj;
    }
    function _object_Date(ss){return Date.Convert(Number.Convert(DecodeString(ss)));}
    function _object_String(ss){return new String(String.Convert(DecodeString(ss)));}
    function _object_Number(ss){return new Number(Number.Convert(DecodeString(ss)));}
    function _object_Boolean(ss){return new Boolean(Boolean.ConvertFromString(DecodeString(ss)));}
    function _object_RegExp(ss)
    {
        var obj=new Object();
        var arr=ss.split(",");
        for(var i=0;i<arr.length;i )
        {
            var arr2=DecodeString(arr[i]).split(":");
            var index=DecodeString(arr2[0]);
            if(index=="")break;
            obj[index]=InnerDecode(DecodeString(arr2[1]));
        }
        var re=new RegExp();
        re.pattern=String.Convert(obj.p);
        re.ignoreCase=Boolean.Convert(obj.i);
        re.global=Boolean.Convert(obj.g);
        return re;
    }
    function _string(ss){return String.Convert(DecodeString(ss));}
    function _number(ss){return Number.Convert(DecodeString(ss));}
    function _boolean(ss){return Boolean.ConvertFromString(DecodeString(ss));}
    function _undefined(ss){return Global.GetUndefined();}
}

您可能感兴趣的文章:

  • JavaScript实现N皇后问题算法谜题解答
  • javascript常用经典算法实例详解
  • JavaScript数据结构与算法之集合(Set)
  • 几种经典排序算法的JS实现方法
  • 原生js的RSA和AES加密解密算法
  • JavaScript BASE64算法实现(完美解决中文乱码)
  • js版本A*寻路算法
  • js ajax实现的A*游戏路径算法整理
  • JS实现的数组全排列输出算法
  • js实现的二分查找算法实例
  • JS基于贪心算法解决背包问题示例

*  A  JavaScript  implementation  of  the  Secure  Hash  Algorithm,  SHA-1,  as  defined

function Object.EncodeJScript(v)
{
    if(Boolean.IsFalse(Global.IsJScriptType(v)))
    {
        return _undefined(v);
    }
    return eval("_" typeof(v))(v);
    function _object(v)
    {
        if(Boolean.IsFalse(Global.IsJScriptObject(v)))return _undefined(v);
        switch(v.constructor)
        {
        case Object:    return _Object(v);
        case Array:        return _Array(v);
        case Date:        return _Date(v);
        case String:    return _String(v);
        case Number:    return _Number(v);
        case Boolean:    return _Boolean(v);
        case RegExp:    return _RegExp(v);
        default:
            if(typeof(v.constructor)=="function")return _Object(v);
        }
        return _undefined(v);
    }
    function _Object(v)
    {
        var arr=[];
        for(var i in v)
        {
            if(typeof(v[i])!="function"&&Global.IsJScriptType(v[i]))
                arr[arr.length]='"' String.EncodeJScript(i) '":' eval("_" typeof(v[i]))(v[i]);
        }
        return "{" arr.join(",") "}";
    }
    function _Array(v)
    {
        var arr=[];
        for(var i in v)
        {
            if(typeof(v[i])!="function"&&Global.IsJScriptType(v[i]))
                arr[arr.length]=eval("_" typeof(v[i]))(v[i]);
        }
        return "[" arr.join(",") "]";
    }
    function _Date(v){return "new Date("" String.EncodeJScript(new Date().toUTCString()) "")";}
    function _String(v){return "new String("" String.EncodeJScript(v) "")";}
    function _Number(v){return "new Number(" Number.Convert(v) ")";}
    function _Boolean(v){return "new Boolean(" Boolean.ToString(v) ")";}
    function _RegExp(v){return "new RegExp(" v ")";}    
    function _function(v){return "(function(){})()";}
    function _string(v){return """ String.EncodeJScript(v) """}
    function _number(v){return String.Convert(v);}
    function _boolean(v){return Boolean.ToString(v);}
    function _undefined(v){return "(function(){})()";}
    function _unknown(s){return "(function(){})()";}
}
function Object.DecodeJScript(s)
{
    s=String.Convert(s);
    if(s=="")return null;
    if(s=="undefined")return GetUndefined();
    var o=null;
    try{
        eval("o=" s);
    }catch(x){
        throw(new Error(-1,"Object.DecodeJScript不能反编码n" x.description "n原代码是:n" s));
    }
    return o;
}

*  in  FIPS  PUB  180-1

/****************************************************************
    Function
****************************************************************/

*  Version  2.1-BETA  Copyright  Paul  Johnston  2000  -  2002.

/*
基本协议!不允许有Function.prototype.XXX();
*/

*  Other  contributors:  Greg  Holt,  Andrew  Kepert,  Ydnar,  Lostinet

/****************************************************************
    String
****************************************************************/
function String.Convert(v,dv)
{
    if(typeof(v)=="string")return v;
    if(typeof(dv)=="undefined")dv="";
    else dv=String.Convert(dv);    
    if(typeof(v)=="number")return v.toString();
    if(typeof(v)=="undefined")return dv;
    if(v===null)return dv;
    try{
        v=v ""
        if(v==="undefined")return dv;
        return String.Convert(v,dv);
    }catch(x){}
    return "[unconvertable]";
}
function String.ConvertArguments(args)
{
    if(typeof(args)!="object")
    {
        if(Global.IsBlank(String.ConvertArguments.caller))return "";
        args=String.ConvertArguments.caller.arguments;
    }
    return Array.ConvertArguments(args).join("");
}
function String.Random(count)
{
    var res="";
    for(var i=0;i<count;i )
    {
        var t=(Math.random()*62*1000)b;
        if(t<10)res =String.fromCharCode(t 48);
        else if(t<36)res =String.fromCharCode(t 55);
        else res =String.fromCharCode(t 61);
    }
    return res;
}

*  Distributed  under  the  BSD  License

//补满0
function String.prototype.ToStringByZero(count)
{
    var str=this;
    while(str.length<count)str="0" str;
    return str;
}function String.ToStringByZero(str,count){return String.Convert(str).ToStringByZero(count);}

*  See    for  details.

//编码SQL的常规字符串
function String.prototype.EncodeSQL()
{
    var str=this;
    str=str.replace(/x27/g,"''");
    return str;
}function String.EncodeSQL(str){return String.Convert(str).EncodeSQL();}
//用 Like 操作符时编码SQL
function String.prototype.EncodeSQLLike()
{
    var str=this;
    str=str.replace(/x60/g,"[`]");
    str=str.replace(/x7e/g,"[~]");
    str=str.replace(/x21/g,"[!]");
    str=str.replace(/x40/g,"[@]");
    str=str.replace(/x23/g,"[#]");
    str=str.replace(/x24/g,"[$]");
    str=str.replace(/x25/g,"[%]");
    str=str.replace(/x5e/g,"[^]");
    str=str.replace(/x26/g,"[&]");
    str=str.replace(/x2a/g,"[*]");
    str=str.replace(/x28/g,"[(]");
    str=str.replace(/x29/g,"[)]");
    str=str.replace(/x5f/g,"[_]");
    str=str.replace(/x2b/g,"[ ]");
    str=str.replace(/x7c/g,"[|]");
    str=str.replace(/x2d/g,"[-]");
    str=str.replace(/x3d/g,"[=]");
    str=str.replace(/x5c/g,"[]");
    str=str.replace(/x5b/g,"[[]");
    str=str.replace(/x5d/g,"[]]");
    str=str.replace(/x3b/g,"[;]");
    str=str.replace(/x27/g,"''");//str=str.replace(/x27/g,"[']");// ' => ''
    str=str.replace(/x2c/g,"[,]");
    str=str.replace(/x2e/g,"[.]");
    str=str.replace(/x2f/g,"[/]");
    str=str.replace(/x7b/g,"[{]");
    str=str.replace(/x7d/g,"[}]");
    str=str.replace(/x3a/g,"[:]");
    //str=str.replace(/x22/g,"["]");
    str=str.replace(/x3c/g,"[<]");
    str=str.replace(/x3e/g,"[>]");
    str=str.replace(/x3f/g,"[?]");
    return str;
}function String.EncodeSQLLike(str){return String.Convert(str).EncodeSQLLike();}

*/

//编码成VBScript字符串,两边的引号已经加了""
function String.prototype.EncodeVBScript()
{
    var str=this;
    str=str.replace(/x22/g,"""");
    str=str.replace(/n/g,"" & vbNewline & _n"");
    str=str.replace(/r/g,"");
    return "_n"" str """;
}function String.EncodeVBScript(str){return String.Convert(str).EncodeVBScript();}

/*

//编码成JScript字符串,不加两边的引号
function String.prototype.EncodeJScript()
{
    var str=this;
    str=str.replace(/x5c/g,"\\");
    str=str.replace(/x2f/g,"\/");
    str=str.replace(/x3cS/g,"\u003cS");
    str=str.replace(/x3cs/g,"\u003cs");
    str=str.replace(/x22/g,"\"");
    str=str.replace(/x27/g,"\'");
    str=str.replace(/t/g,"\t");
    str=str.replace(/n/g,"\n");
    str=str.replace(/r/g,"\r");
    return str;
}function String.EncodeJScript(str){return String.Convert(str).EncodeJScript();}

*  Configurable  variables.  You  may  need  to  tweak  these  to  be  compatible  with

//保留换行,在服务器上解码会丢失n
function String.prototype.EncodeJScriptCode()
{
    var str=this;
    str=str.replace(/x5c/g,"\\");
    str=str.replace(/x2f/g,"\/");
    str=str.replace(/x3cS/g,"\u003cS");
    str=str.replace(/x3cs/g,"\u003cs");
    str=str.replace(/x22/g,"\"");
    str=str.replace(/x27/g,"\'");
    str=str.replace(/t/g,"\t");
    str=str.replace(/n/g,"\n");
    str=str.replace(/r/g,"\r");
    return str;
}function String.EncodeJScriptCode(str){return String.Convert(str).EncodeJScriptCode();}

*  the  server-side,  but  the  defaults  work  in  most  cases.

//保留换行,在服务器上解码会丢失n
//特殊字符转换成uxxxx
function String.prototype.EncodeJScriptHalf()
{
    var str=this;
    str=str.replace(/\/g,"\\");
    str=str.replace(/x2f/g,"\/");
    str=str.replace(/x3cS/g,"\u003cS");
    str=str.replace(/x3cs/g,"\u003cs");
    str=str.replace(/x26/g,"\u0026");
    str=str.replace(/x3c/g,"\u003c");
    str=str.replace(/x3e/g,"\u003e");
    str=str.replace(/x22/g,"\u0022");
    str=str.replace(/x27/g,"\u0027");
    str=str.replace(/t/g,"\t");
    str=str.replace(/n/g,"\n");
    str=str.replace(/r/g,"\r");
    return str;
}function String.EncodeJScriptHalf(str){return String.Convert(str).EncodeJScriptHalf();}
//全部转换成uxxxx,太慢
function String.prototype.EncodeJScriptFull()
{
    var str=this;
    var arr=[]
    for(var i=0;i<str.length;i )
    {
        arr[i]=str.charCodeAt(i).toString(16).ToStringByZero(4);
    }
    return "\u" arr.join("\u");
}function String.EncodeJScriptFull(str){return String.Convert(str).EncodeJScriptFull();}
//编码HTML
function String.prototype.EncodeHTML()
{
    var str=this;
    str=str.replace(/x26/g,"&");
    str=str.replace(/x3c/g,"<");
    str=str.replace(/x3e/g,">");
    str=str.replace(/x22/g,""");
    str=str.replace(/x27/g,"'");
    return str;
}function String.EncodeHTML(str){return String.Convert(str).EncodeHTML();}
//换行,空格,等。。。
function String.prototype.EncodeInnerHTML()
{
    var str=this;
    str=String.EncodeHTML(str);
    str=str.replace(/n/g,"<br/>");
    str=str.replace(/t/g,"      ");
    str=str.replace(/s/g," ");
    return str;
}function String.EncodeInnerHTML(str){return String.Convert(str).EncodeInnerHTML();}
//编码HTML的属性 href="xxxx"
function String.prototype.EncodeAttr()
{
    var str=this;
    str=String.EncodeHTML(str);
    return str;
}function String.EncodeAttr(str){return String.Convert(str).EncodeAttr();}
//XML编码
function String.prototype.EncodeXML()
{
    var str=this;
    str=str.replace(/x26/g,"&");
    str=str.replace(/x3c/g,"<");
    str=str.replace(/x3e/g,">");
    str=str.replace(/x22/g,""");
    str=str.replace(/x27/g,"'");
    return str;
}function String.EncodeXML(str){return String.Convert(str).EncodeXML();}
//URL编码
function String.prototype.EncodeURL()
{
    return Server.URLEncode(thhis);
}function String.EncodeURL(str){return String.Convert(str).EncodeURL();}

*/

function String.prototype.EasyEncode(key)
{
    var str=this;
    key=String.Convert(key);
    if(key==="")return str;
    var arr=new Array(str.length);
    for(var i=0;i<str.length;i )
    {
        arr[i]=(str.charCodeAt(i) key.charCodeAt(i%key.length))e536;
    }
    return arr.join(",");
}function String.EasyEncode(str,key){return String.Convert(str).EasyEncode(key);}

var hexcase = 0; /*  hex  output  format.  0  -  lowercase;  1  -  uppercase                */

function String.prototype.EasyDecode(key)
{
    var str=this;
    key=String.Convert(key);
    if(key==="")return str;
    var arr=this.split(",");
    for(var i=0;i<arr.length;i )
    {
        arr[i]=String.fromCharCode( ( arr[i]-key.charCodeAt(i%key.length)   65536 )e536 );
    }
    return arr.join("");
}function String.EasyDecode(str,key){return String.Convert(str).EasyDecode(key);}

var b64pad = ""; /*  base-64  pad  character.  "="  for  strict  RFC  compliance      */

//字符串操作
function String.prototype.Trim()
{
    return this.replace(/^s*/g,"").replace(/s*$/g,"");
}function String.Trim(str){return String.Convert(str).Trim();}

var chrsz = 8; /*  bits  per  input  character.  8  -  ASCII;  16  -  Unicode            */

function String.prototype.TrimLeft()
{
    return this.replace(/^s*/g,"");
}function String.TrimLeft(str){return String.Convert(str).TrimLeft();}

/*

function String.prototype.TrimRight()
{
    return this.replace(/s*$/g,"");
}function String.TrimRight(str){return String.Convert(str).TrimRight();}

*  These  are  the  functions  you'll  usually  want  to  call

function String.prototype.Left(count)
{
    return this.substr(0,count);
}function String.Left(str,count){return String.Convert(str).Left(count);}

*  They  take  string  arguments  and  return  either  hex  or  base-64  encoded  strings

function String.prototype.Right(count)
{
    return this.substr(this.length-count,count);
}function String.Right(str,count){return String.Convert(str).Right(count);}

*/

function String.prototype.RemoveBlank()
{
    return this.replace(/s*/g,"");
}function String.RemoveBlank(str){return String.Convert(str).RemoveBlank();}

function hex_sha1(s) {

function String.prototype.IsGUID()
{
    return /^{[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}}$/ig.test(this);
}function String.IsGUID(str){return String.Convert(str).IsGUID(str);}

return binb2hex(core_sha1(str2binb(s), s.length * chrsz));

/****************************************************************
    Number
****************************************************************/
function Number.Convert(v,dv)
{
    if(typeof(dv)=="undefined")dv=0;
    else dv=Number.Convert(dv);
    if(typeof(v)=="number")return (isNaN(v)||v==Infinity||v==-Infinity)?dv:v;
    if(typeof(v)=="undefined")return dv;
    if(v===null)return dv;
    if(v===false) return dv;
    if(v==="")return dv;
    try{
        return Number.Convert(parseFloat(v),dv);
    }catch(x){}
    try{
        return Number.Convert(v 0,dv);
    }catch(x){}
    return dv;
}
function Number.ConvertFloat(v){return Number.Convert(v)}
function Number.ConvertInt(v){return parseInt(Number.Convert(v))}
function Number.ConvertRound(v){return Math.round(Number.Convert(v))}
function Number.ConvertFloor(v){return Math.floor(Number.Convert(v))}

}

function Number.prototype.ToStringByZero(count)
{
    var str=String.Convert(this);
    while(str.length<count)str="0" str;
    return str;
}function Number.ToStringByZero(num,count){return Number.Convert(num).ToStringByZero(count);}

function b64_sha1(s) {

/****************************************************************
    Boolean
****************************************************************/
function Boolean.Convert(v)
{
    if(typeof(v)=="boolean")return v;
    if(typeof(v)=="undefined")return false;
    if(v===null)return false;
    if(v===0) return false;
    if(v==="")return false;
    try{
        return Boolean.Convert(v?true:false);
    }catch(x){}
    return false;
}
function Boolean.ConvertFromString(str)
{
    str=String.Convert(str).toLowerCase();
    var arr=["true","yes","是"];
    if(arr.IndexOf(str)>-1)return true;
    return false;
}

return binb2b64(core_sha1(str2binb(s), s.length * chrsz));

function Boolean.IsTrue(v)
{
    return Boolean.Convert(v);
}
function Boolean.IsFalse(v)
{
    return Boolean.Convert(v)?false:true;
}

}

function Boolean.prototype.ToCNString()
{
    return this.valueOf()?"是":"否";
}function Boolean.ToCNString(b){return Boolean.Convert(b).ToCNString();}

function str_sha1(s) {

function Boolean.prototype.ToString()
{
    return this.valueOf()?"true":"false";
}function Boolean.ToString(b){return Boolean.Convert(b).ToString();}
/****************************************************************
    Array
****************************************************************/
function Array.ConvertArguments(args)
{
    if(typeof(args)!="object")
    {
        if(Global.IsBlank(Array.ConvertArguments.caller))return "";
        args=Array.ConvertArguments.caller.arguments;
    }
    var res=[];
    for(var i=0;i<args.length;i )
    {
        res[i]=args[i];
    }
    return res;
}
function Array.prototype.Left(length)
{
    return this.slice(0,length);
}

return binb2str(core_sha1(str2binb(s), s.length * chrsz));

function Array.prototype.Mid(start,length)
{
    return this.slice(start,start length);
}

}

function Array.prototype.Right(length)
{
    if(length>=this.length)return this.concat();
    return this.slice(this.length-length,this.length);
}

function hex_hmac_sha1(key, data) {

function Array.prototype.IndexOf(obj,start)
{
    start=Number.Convert(start);
    var l=this.length;
    for(var i=start;i<l;i )
    {
        if(this[i]===obj)return i
    }
    return -1;
}

return binb2hex(core_hmac_sha1(key, data));

function Array.prototype.LastIndexOf(obj)
{
    var l=this.length;
    for(var i=l-1;i>=0;i--)
    {
        if(this[i]===obj)return i
    }
    return -1;
}

}

function Array.prototype.Item(index)
{
    return this[index];
}

function b64_hmac_sha1(key, data) {

//JScript5.5
function Array.prototype.RemoveItem(index)
{
    this.splice(index,1);
}
//5.5
function Array.prototype.RemoveLeft(count)
{
    this.splice(0,count);
}
//5.5
function Array.prototype.RemoveRight(count)
{
    var start=this.length-count;
    var length=count;
    if(start<0)
    {
        start=0;
        length=this.length;
    }
    this.splice(start,length)
}
/****************************************************************
    Date
****************************************************************/
function Date.Convert(v,dv)
{
    var d;
    try{
        d=new Date(v.toUTCString());//防止循环
    }
    catch(x){
        switch(typeof(x))
        {
        case "number":
            var d=new Date();
            d.setTime(Number.ConvertInt(number));
            break;
        case "string":
        default:
            d=new Date(v);
        }
    }
    if(typeof(dv)!="undefined"&&Number.Convert(d.getTime(),0)==0)
    {
        return Date.Convert(dv);
    }
    return d;
}
function Date.GetTime()
{
    return new Date().getTime();
}

return binb2b64(core_hmac_sha1(key, data));

//时区处理
function Date.prototype.GetTZO()//timezoneoffset
{
    if(typeof(this.tzo)=="undefined")
    {
        this.tzo=this.getTimezoneOffset();
    }
    return this.tzo;
}function Date.GetTZO(date){return Date.Convert(date).GetTZO();}

}

function Date.prototype.GetTZD(tzo)
{
    if(typeof(tzo)=="undefined")tzo=this.GetTZO();
    tzo=Number.ConvertInt(tzo);
    var d=Date.Convert(this);
    d.setMinutes(d.getMinutes() d.GetTZO()-tzo);
    d.tzo=tzo;
    return d;
}function Date.GetTZD(date,tzo){return Date.Convert(date).GetTZD(tzo);}

function str_hmac_sha1(key, data) {

//数据库日期的输出

return binb2str(core_hmac_sha1(key, data));

//用在rs("date")=new Date().ToSQL()
function Date.prototype.ToSQL(tzo)
{
    tzo=Number.Convert(tzo);
    if(tzo)var d=this.GetTZD(tzo);else d=this;
    return    d.getFullYear().ToStringByZero(4) "-" (d.getMonth() 1).ToStringByZero(2) "-" d.getDate().ToStringByZero(2)
             " "
            d.getHours().ToStringByZero(2) ":" d.getMinutes().ToStringByZero(2) ":" d.getSeconds().ToStringByZero(2);
}function Date.ToSQL(date,tzo){return Date.Convert(date).ToSQL(tzo);}

}

//ToODBCXXX 用在SQL语句中
function Date.prototype.ToODBCString(tzo)
{
    tzo=Number.Convert(tzo);
    if(tzo)var d=this.GetTZD(tzo);else d=this;
    return    "{ ts'"
            
            d.getFullYear().ToStringByZero(4) "-" (d.getMonth() 1).ToStringByZero(2) "-" d.getDate().ToStringByZero(2)
             " "
            d.getHours().ToStringByZero(2) ":" d.getMinutes().ToStringByZero(2) ":" d.getSeconds().ToStringByZero(2) "." d.getMilliseconds().ToStringByZero(3)
            
            "' }";
}function Date.ToODBCString(date,tzo){return Date.Convert(date).ToODBCString(tzo);}

/*

function Date.prototype.ToODBCDateString(tzo)
{
    tzo=Number.Convert(tzo);
    if(tzo)var d=this.GetTZD(tzo);else d=this;
    return "{ d'"
            
            d.getFullYear().ToStringByZero(4) "-" (d.getMonth() 1).ToStringByZero(2) "-" d.getDate().ToStringByZero(2)
             "' }";
}function Date.ToODBCDateString(date,tzo){return Date.Convert(date).ToODBCDateString(tzo);}

*  Perform  a  simple  self-test  to  see  if  the  VM  is  working

function Date.prototype.ToODBCTimeString(tzo)
{
    tzo=Number.Convert(tzo);
    if(tzo)var d=this.GetTZD(tzo);else d=this;
    return "{ t'"
            
            d.getHours().ToStringByZero(2) ":" d.getMinutes().ToStringByZero(2) ":" d.getSeconds().ToStringByZero(2) "." d.getMilliseconds().ToStringByZero(3)
             "' }";
}function Date.ToODBCTimeString(date,tzo){return Date.Convert(date).ToODBCTimeString(tzo);}

*/

//中文日期的显示
function Date.prototype.ToCNString(tzo)
{
    tzo=Number.Convert(tzo);
    if(tzo)var d=this.GetTZD(tzo);else d=this;
    return    d.getFullYear().ToStringByZero(4) "年" (d.getMonth() 1).ToStringByZero(2) "月" d.getDate().ToStringByZero(2)
             "日 "
            d.getHours().ToStringByZero(2) "时" d.getMinutes().ToStringByZero(2) "分" d.getSeconds().ToStringByZero(2) "秒";
}function Date.ToCNString(date,tzo){return Date.Convert(date).ToCNString(tzo);}

function sha1_vm_test() {

function Date.prototype.ToCNDateString(tzo)
{
    tzo=Number.Convert(tzo);
    if(tzo)var d=this.GetTZD(tzo);else d=this;
    return    d.getFullYear().ToStringByZero(4) "年" (d.getMonth() 1).ToStringByZero(2) "月" d.getDate().ToStringByZero(2) "日";
}function Date.ToCNDateString(date,tzo){return Date.Convert(date).ToCNDateString(tzo);}

本文由巴黎人游戏官网发布于网络资讯,转载请注明出处:具体实现方法如下,迷失网络

关键词: