Skip to content

Commit b3614c3

Browse files
committed
calculateHash is non-failing operation. Fixes #664
1 parent 5809ca0 commit b3614c3

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

Sources/CryptoSwift/HMAC.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public final class HMAC: Authenticator {
3737
}
3838
}
3939

40-
func calculateHash(_ bytes: Array<UInt8>) -> Array<UInt8>? {
40+
func calculateHash(_ bytes: Array<UInt8>) -> Array<UInt8> {
4141
switch self {
4242
case .sha1:
4343
return Digest.sha1(bytes)
@@ -72,9 +72,8 @@ public final class HMAC: Authenticator {
7272
self.key = key
7373

7474
if key.count > variant.blockSize() {
75-
if let hash = variant.calculateHash(key) {
76-
self.key = hash
77-
}
75+
let hash = variant.calculateHash(key)
76+
self.key = hash
7877
}
7978

8079
if key.count < variant.blockSize() {
@@ -94,10 +93,8 @@ public final class HMAC: Authenticator {
9493
ipad[idx] = key[idx] ^ ipad[idx]
9594
}
9695

97-
guard let ipadAndMessageHash = variant.calculateHash(ipad + bytes),
98-
let result = variant.calculateHash(opad + ipadAndMessageHash) else {
99-
throw Error.authenticateError
100-
}
96+
let ipadAndMessageHash = variant.calculateHash(ipad + bytes)
97+
let result = variant.calculateHash(opad + ipadAndMessageHash)
10198

10299
// return Array(result[0..<10]) // 80 bits
103100
return result

Sources/CryptoSwift/PKCS/PBKDF1.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public extension PKCS5 {
3737
}
3838
}
3939

40-
fileprivate func calculateHash(_ bytes: Array<UInt8>) -> Array<UInt8>? {
40+
fileprivate func calculateHash(_ bytes: Array<UInt8>) -> Array<UInt8> {
4141
switch self {
4242
case .sha1:
4343
return Digest.sha1(bytes)
@@ -67,9 +67,7 @@ public extension PKCS5 {
6767
throw Error.derivedKeyTooLong
6868
}
6969

70-
guard let t1 = variant.calculateHash(password + salt) else {
71-
throw Error.invalidInput
72-
}
70+
let t1 = variant.calculateHash(password + salt)
7371

7472
self.iterations = iterations
7573
self.variant = variant
@@ -81,7 +79,7 @@ public extension PKCS5 {
8179
public func calculate() -> Array<UInt8> {
8280
var t = t1
8381
for _ in 2...iterations {
84-
t = variant.calculateHash(t)!
82+
t = variant.calculateHash(t)
8583
}
8684
return Array(t[0..<self.keyLength])
8785
}

0 commit comments

Comments
 (0)