Signature Algorithm

Here are the steps of how to generate a signature:

  1. Sort all non-empty parameters by name ascendingly (exclude sign parameter).
  2. Combine above parameters with its name and value sequentially into a string.
  3. Hash the string with MD5 algorithm, and then make lowercase of it.
  4. Append PrivateKey to the string, and repeat the step above. There is your signature.

Sample Codes

  • Take GetOrder as an example, combine gamekey, openid, orderid parameters name-value into a string, hash with MD5 algorithm, and make lowercase of it.
  • Append PrivateKey to the string, hash with MD5 algorithm, and make lowercase of it. There is your signature.
  • node.js sample code :
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    var crypto = require('crypto');
    
    var myPrivateKey = 'my_private_key';
    var signData = {
        orderid: 'my_orderid',
        gamekey: 'my_gamekey',
        openid: 'my_openid'
    };
    
    var sign = get_sign(signData, myPrivateKey);
    
    function get_md5(data) {
        data = new Buffer(data).toString("binary");
        return crypto.createHash('md5').update(data).digest('hex').toLowerCase();
    }
    
    function get_sign(post, privateKey) {
        var keys = [];
    
        for (key in post) {
            keys.push(key);
        }
    
        keys = keys.sort();
        var paramString = '';
        for (i in keys) {
            paramString += post[keys[i]];
        }
    
        var s1 = get_md5(paramString);
        var p2 = s1 + privateKey;
        var s2 = get_md5(p2);
        return  s2;
    }
    
    function check_sign (post, privateKey) {
        var source_sign = post.Sign;
        delete post.Sign;
        var new_sign = get_sign(post, privateKey);
    
        if (source_sign == new_sign) {
            return true;
        }
    
        return false;
    }