Swift國(guó)內(nèi)社區(qū): SwiftMic
Vapor 內(nèi)置支持 Hash 逻恐。
示例
想要獲取一個(gè)字符串的 Hash 值像吻,只需使用 Droplet
中的 hash
即可。
let hashed = drop.hash.make("vapor")
print("\(hashed)")
輸出
ac774af08cfe40f22367bad426cacb321da40fa28daeefb745e251defd365d20
SHA2Hasher
默認(rèn)情況下复隆, Vapor 使用 256 位的 SHA2Hasher 拨匆,不過(guò)也可以通過(guò)如下方式更改。
let sha512 = SHA2Hasher(variant: .sha512)
let drop = Droplet(hash: sha512)
自定義 Hash
Vapor 也支持自定義 Hash 挽拂,只需要遵循 Hash
協(xié)議即可惭每。
public protocol Hash: class {
/**
A string used to add an additional
layer of security to all hashes
*/
var key: String { get set }
/**
Given a string, this function will
return the hashed string according
to whatever algorithm it chooses to implement.
*/
func make(_ string: String) -> String
}
Go to Vapor系列教程 - 目錄