Skip to content

Commit

Permalink
Update APP.md (#911)
Browse files Browse the repository at this point in the history
添加了Swift语言的支持
  • Loading branch information
Lightning-Lion authored Dec 21, 2023
1 parent 968b3e2 commit faa3d97
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion docs/misc/sign/APP.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

## Demo

该 Demo 提供 [Python](#Python)[Java](#Java) 语言例程
该 Demo 提供 [Python](#Python)[Java](#Java) [TS/JS](#TypeScript/JavaScript)[Swift](#Swift) 语言例程

使用 appkey = `1d8b6e7d45233436`, appsec = `560c52ccd288fed045859ed18bffd973` 对如下 `params` 参数进行签名

Expand Down Expand Up @@ -166,3 +166,51 @@ console.log(
```

输出结果为:01479cf20504d865519ac50f33ba3a7d

### Swift

```swift
import Foundation
import CommonCrypto

//Swift标准库没有MD5函数,所以我们要自己实现一个
func MD5(string: String) -> String {
let length = Int(CC_MD5_DIGEST_LENGTH)
var digest = [UInt8](repeating: 0, count: length)

if let d = string.data(using: .utf8) {
_ = d.withUnsafeBytes { body -> String in
CC_MD5(body.baseAddress, CC_LONG(d.count), &digest)
return ""
}
}

return (0..<length).reduce("") {
$0 + String(format: "%02x", digest[$1])
}
}

func appSign(params: [String:String],appKey:String,appSec:String) -> String {
var signedParams = params
signedParams["appkey"] = appKey
let sortedParams = signedParams.sorted { $0.key < $1.key }
//在制作成query时,需要显式addingPercentEncoding转换
let query = sortedParams.map { "\($0.key)=\($0.value.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)" }.joined(separator: "&")
let sign = MD5(string: query+appSec)
return sign
}


//testSign
let appKey = "1d8b6e7d45233436"
let appSec = "560c52ccd288fed045859ed18bffd973"
let signResult = appSign(params: [
"id": "114514",
"str": "1919810",
"test": "いいよ,こいよ",
],appKey:appKey,appSec:appSec)
print(signResult)
```

输出结果为:01479cf20504d865519ac50f33ba3a7d

0 comments on commit faa3d97

Please sign in to comment.