sign.js

var m = require("./md5.js");

function sort(data)
{
    var keys = Object.keys(data).sort();
    var result = {};
    for(var i = 0; i < keys.length; i++){
        var key = keys[i];
        result[key] = data[key];
    }
    return result;
}

function jsonVAL(json)
{
    var v = '';
    for(var i in json){
        v += json[i];
    }
    return v;
}

function getTimestamp()
{
    return Date.parse(new Date()) / 1000;
}

function mtRand(min, max)
{
    var result = Math.random() * (max - min + 1) + min;
    return parseInt(result);
}

function sign(appkey, json)
{
    json.timestamp = getTimestamp();
    json.rand = mtRand(100000, 999999);
    json.appkey = appkey;
    json.sign = m.md5(jsonVAL(sort(json)));
    delete json.appkey;
    return json;
}

var x = {
    sign: sign
};
module.exports = x;

xxfaxy
1.6k 声望18 粉丝