Signature Method

1. Signature Method

The DNS API authenticates every request. Each request must include a signature (hash) in the common request parameters to verify the user’s identity. The signature is generated using the user's security credentials, which include apiKey and apiSecret. If you do not have security credentials, you must apply for them on the 51DNS official website; otherwise, you cannot call the API.

2. Obtaining Security Credentials

Before using the DNS API for the first time, users need to check their security credentials at API Key Management. The security credentials include:

  • apiKey: Identifies the API caller.
  • apiSecret: The key used to encrypt the signature string and verify it on the server side.

3. Generating the Signature String

With apiKey and apiSecret, you can generate the signature string. The process is as follows:

Sort Parameters

Sort all request parameters in ascending order by parameter name (dictionary order). This is similar to how words are arranged in a dictionary: first by the first character, then by the second if the first is identical, and so on. You can use built-in sorting functions, such as ksort in PHP.

{
  "apiKey":"c7722149110b7492a2e5cf1d8f3f966b",
  "domain":"dns.com",
  "timestamp":"1521005892"
}

When developing with other programming languages, you can sort the parameters in the example above; as long as the resulting order is the same, the signature will be valid.

Concatenate Parameters

Format the sorted parameters as parameterName=parameterValue. For example, for the domain parameter, "domain=dns.com". Then append apiSecret to the end of the string.


apiSecret=ecb4ff0e877a83292b9f35067e9ae673

Request string:
apiKey=c7722149110b7492a2e5cf1d8f3f966b&domain=dns.com
&timestamp=1521005892

Final string:
apiKey=c7722149110b7492a2e5cf1d8f3f966b&domain=dns.com
&timestamp=1521005892ecb4ff0e877a83292b9f35067e9ae673

Generate the Signature

Use the signature algorithm (MD5) on the final string from Step 2 to generate the signature.

Example in PHP:

$lastStr = 'apiKey=c7722149110b7492a2e5cf1d8f3f966b&domain=dns.com
&timestamp=1521005892';
$signStr = md5($lastStr . 'ecb4ff0e877a83292b9f35067e9ae673');
echo $signStr;

Resulting signature:

0eb4933a634000ce215370683d6f1338
  1. Signature Demo {#4}

PHP

/**
* 签名方法
* @param $parameters
* @param $apiSecret
* @return string
*/
function getHash($parameters,$apiSecret){
    ksort($parameters);
    $hashString = '';
    foreach($parameters as $key => $value){
        $hashString .= ($hashString ? '&' : '') . $key . '=' . $value;
    }
    return md5($hashString . $apiSecret);;
}

$parameters = [
    'domain' => 'dns.com',
    'timestamp' => time(),
    'apiKey' => 'your api_key'
];

echo getHash($parameters,'your api_secret');

GO

package main

import (
    "crypto/md5"
    "encoding/hex"
    "fmt"
    "sort"
)

func GetHash(param map[string]string, secert string) string {
    var keyList []string
    for k, _ := range param {
        keyList = append(keyList, k)
    }
    sort.Strings(keyList)
    var hashString string
    for _, key := range keyList {
        if hashString == "" {
            hashString += key + "=" + param[key]
        } else {
            hashString += "&" + key + "=" + param[key]
        }
    }

    m := md5.New()
    m.Write([]byte(hashString + secert))
    cipherStr := m.Sum(nil)
    return hex.EncodeToString(cipherStr)
}

func main() {
    var param = map[string]string{}
    param["domain"] = "dns.com"
    param["timestamp"] = "1602734019"
    param["apiKey"] = "your api_key"
    fmt.Println(GetHash(param, "your api_secret"))
}

Python

import hashlib

def GetHash(param, secert):
    hasString = ""
    for key in sorted(param):
        if hasString == "":
            hasString = key + "=" + param[key]
        else:
            hasString += "&" + key + "=" + param[key]
    hasString = hasString + secert
    signStr = hasString.encode(encoding='utf-8')
    sign = hashlib.md5(signStr).hexdigest()
    return sign

if __name__ == '__main__':
    param = {"domain": "dns.com", "timestamp": "1602734019","apiKey": "your api_key"}
    print(GetHash(param, "your api_secret"))

Java

package com.dns;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

public class App {
    public static String GetHash(Map<String, String> param , String secert) throws NoSuchAlgorithmException{
        Object[] keyList = param.keySet().toArray();
        Arrays.sort(keyList);
        String hashString = "";
        for   (int i = 0; i < keyList.length;   i++) {
            String key = keyList[i].toString();
            if (hashString == "") {
                hashString += key + "=" + param.get(key);
            }else{
                hashString += "&" + key + "=" + param.get(key);
            }
           param.get(keyList[i]);
        }
        hashString += secert;
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(StandardCharsets.UTF_8.encode(hashString));
            return String.format("%032x", new BigInteger(1, md5.digest()));
        }catch (Exception e) {
            throw e;
        }
    }

    public static void main(String[] args) {
        Map param =  new HashMap<String, String>();
        param.put("domain","dns.com");
        param.put("timestamp","1602734019");
        param.put("apiKey","your api_key");
        String rsp = "";
        try {
            rsp = GetHash(param,"your api_secret");
        }catch (Exception e){ }
        System.out.println(rsp);
    }
}

C#(csharp)

using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace checksum
{
    class MD5Hash
    {
        public static string GetHash(Dictionary<string, string> param, string apiSecret)
        {
            var keyList = new List<string>();
            foreach (var item in param)
            {
                keyList.Add(item.Key);
            }
            keyList.Sort(StringComparer.Ordinal);
            string hashString = "";
            foreach (var key in keyList)
            {
               if (hashString == "")
                {
                    hashString += key + "=" + param[key];
                }
                else
                {
                    hashString += "&" + key + "=" + param[key];
                }
            }
            return MD5Hash.createMD5(hashString + apiSecret);
        }

        public static string createMD5(string input)
        {
            MD5 md5 = MD5.Create();
            byte[] inputBytes = Encoding.ASCII.GetBytes(input);
            byte[] hashBytes = md5.ComputeHash(inputBytes);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hashBytes.Length; i++)
            {
                sb.Append(hashBytes[i].ToString("X2"));
            }
            return sb.ToString().ToLower();
        }
    }
}
DNS Jude
DNS Puff
DNS Grace
DNS Sugar
DNS Amy
DNS NOC
Title
Email Address
Type
Information
Code
Submit