@@ -105,7 +105,7 @@ public class Client {
105
105
let iterations = srpInit. iteration
106
106
107
107
do {
108
- guard let encryptedPassword = self . pbkdf2 ( password: password, saltData: decodedSalt, keyByteCount: 32 , prf: CCPseudoRandomAlgorithm ( kCCPRFHmacAlgSHA256) , rounds: iterations) else {
108
+ guard let encryptedPassword = self . pbkdf2 ( password: password, saltData: decodedSalt, keyByteCount: 32 , prf: CCPseudoRandomAlgorithm ( kCCPRFHmacAlgSHA256) , rounds: iterations, protocol : srpInit . protocol ) else {
109
109
throw Error . srpInvalidPublicKey
110
110
}
111
111
@@ -382,9 +382,14 @@ public class Client {
382
382
return Data ( hash)
383
383
}
384
384
385
- private func pbkdf2( password: String , saltData: Data , keyByteCount: Int , prf: CCPseudoRandomAlgorithm , rounds: Int ) -> Data ? {
385
+ private func pbkdf2( password: String , saltData: Data , keyByteCount: Int , prf: CCPseudoRandomAlgorithm , rounds: Int , protocol srpProtocol : SRPProtocol ) -> Data ? {
386
386
guard let passwordData = password. data ( using: . utf8) else { return nil }
387
- let hashedPasswordData = sha256 ( data: passwordData)
387
+ let hashedPasswordDataRaw = sha256 ( data: passwordData)
388
+ let hashedPasswordData = switch srpProtocol {
389
+ case . s2k: hashedPasswordDataRaw
390
+ // the legacy s2k_fo protocol requires hex-encoding the digest before performing PBKDF2.
391
+ case . s2k_fo: Data ( hashedPasswordDataRaw. hexEncodedString ( ) . lowercased ( ) . utf8)
392
+ }
388
393
389
394
var derivedKeyData = Data ( repeating: 0 , count: keyByteCount)
390
395
let derivedCount = derivedKeyData. count
@@ -515,4 +520,10 @@ public struct ServerSRPInitResponse: Decodable {
515
520
let salt : String
516
521
let b : String
517
522
let c : String
523
+ let `protocol` : SRPProtocol
524
+ }
525
+ extension Data {
526
+ func hexEncodedString( ) -> String {
527
+ return map { String ( format: " %02hhx " , $0) } . joined ( )
528
+ }
518
529
}
0 commit comments