最近遇到好多要生成 sign 的接口,要求使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串,最后拼接上key进行MD5加密。
规则:
- . ◆ 参数名ASCII码从小到大排序(字典序);
- . ◆ 如果参数的值为空不参与签名;
- . ◆ 参数名区分大小写;
- . ◆ 参数名Sign 、Key 不参与拼接字符串;
发现VB6.0 的资料很少,特此整理一份网上的代码。
转自:http://www.mzwu.com/article.asp?id=2245
'说明:Dictionary排序 '参数: ' objDict:Dictionary对象 ' intSort: 1 根据key排序; 2 根据value排序 Function SortDictionary(objDict, intSort) ' declare our variables Dim strDict() Dim objKey Dim strKey, strItem Dim X, Y, Z ' get the dictionary count Z = objDict.Count ' we need more than one item to warrant sorting If Z > 1 Then ' create an array to store dictionary information ReDim strDict(Z, 2) X = 0 ' populate the string array For Each objKey In objDict strDict(X, 1) = CStr(objKey) strDict(X, 2) = CStr(objDict(objKey)) X = X + 1 Next ' perform a a shell sort of the string array For X = 0 To (Z - 2) For Y = X To (Z - 1) If StrComp(strDict(X, intSort), strDict(Y, intSort), vbTextCompare) > 0 Then strKey = strDict(X, 1) strItem = strDict(X, 2) strDict(X, 1) = strDict(Y, 1) strDict(X, 2) = strDict(Y, 2) strDict(Y, 1) = strKey strDict(Y, 2) = strItem End If Next Next ' erase the contents of the dictionary object objDict.RemoveAll ' repopulate the dictionary with the sorted information For X = 0 To (Z - 1) If LCase(strDict(X, 1)) <> "sign" And LCase(strDict(X, 1)) <> "key" And strDict(X, 2) <> "" Then objDict.Add strDict(X, 1), strDict(X, 2) End If Next End If End Function
使用方法:
Dim dict As Dictionary Dim item Dim Return as String Set dict = New Dictionary dict.Add "aaa", "094959" dict.Add "ccc", "0000000000" dict.Add "fff", "20180912"
SortDictionary dict, 1 '排序 For Each item In dict Return = Return & item & "=" & dict(item) & "&" Next
Return = Return & "key=keyvalue"
Debug.Print Return