Add support for SHA256 fingerprints (#198)

* Add SHA256

* Update tests

* Fix padding
This commit is contained in:
Max Goedjen
2021-01-17 16:16:38 -08:00
committed by GitHub
parent 0544287141
commit 4de805dd37
3 changed files with 25 additions and 6 deletions

View File

@@ -19,7 +19,15 @@ public struct OpenSSHKeyWriter {
.joined(separator: " ")
}
public func openSSHFingerprint<SecretType: Secret>(secret: SecretType) -> String {
public func openSSHSHA256Fingerprint<SecretType: Secret>(secret: SecretType) -> String {
// OpenSSL format seems to strip the padding at the end.
let base64 = Data(SHA256.hash(data: data(secret: secret))).base64EncodedString()
let paddingRange = base64.index(base64.endIndex, offsetBy: -2)..<base64.endIndex
let cleaned = base64.replacingOccurrences(of: "=", with: "", range: paddingRange)
return "SHA256:\(cleaned)"
}
public func openSSHMD5Fingerprint<SecretType: Secret>(secret: SecretType) -> String {
Insecure.MD5.hash(data: data(secret: secret))
.compactMap { ("0" + String($0, radix: 16, uppercase: false)).suffix(2) }
.joined(separator: ":")