Skip to content

Commit 9e7d683

Browse files
committed
Fix a couple of issues
1 parent 9144bf4 commit 9e7d683

3 files changed

Lines changed: 18 additions & 5 deletions

File tree

Sources/Bcrypt/Base64.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
enum Base64Error: Error {
2+
case invalidCharacter
3+
}
4+
15
@usableFromInline
26
struct Base64 {
37
@usableFromInline
@@ -74,7 +78,7 @@ struct Base64 {
7478
}
7579

7680
@usableFromInline
77-
static func decode(_ s: [UInt8], count maxolen: Int) -> [UInt8] {
81+
static func decode(_ s: [UInt8], count maxolen: Int) throws(Base64Error) -> [UInt8] {
7882
var off = 0
7983
var olen = 0
8084
var result = [UInt8](repeating: 0, count: maxolen)
@@ -91,7 +95,7 @@ struct Base64 {
9195
c2 = char64(of: s[off])
9296
off &+= 1
9397
if c1 == UInt8.max || c2 == UInt8.max {
94-
break
98+
throw .invalidCharacter
9599
}
96100

97101
o = c1 &<< 2
@@ -106,7 +110,7 @@ struct Base64 {
106110
off &+= 1
107111

108112
if c3 == UInt8.max {
109-
break
113+
throw .invalidCharacter
110114
}
111115

112116
o = (c2 & 0x0f) &<< 4

Sources/Bcrypt/Hasher.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,16 @@ extension Bcrypt {
4848
throw BcryptError.invalidSaltLength
4949
}
5050

51-
let cSalt = Base64.decode(salt, count: Self.maxSalt)
51+
let cSalt: [UInt8]
52+
do {
53+
cSalt = try Base64.decode(salt, count: Self.maxSalt)
54+
} catch {
55+
throw BcryptError.invalidSalt
56+
}
57+
58+
guard cSalt.count == 16 else {
59+
throw BcryptError.invalidSalt
60+
}
5261

5362
guard password.count > 0 else {
5463
throw BcryptError.emptyPassword

Sources/Bcrypt/Verifier.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ extension Bcrypt {
4949
guard a.count == b.count else { return false }
5050
var areEqual: UInt8 = 0
5151
var i = a.count - 1
52-
while i != 0 {
52+
while i >= 0 {
5353
areEqual |= a[i] ^ b[i]
5454
i -= 1
5555
}

0 commit comments

Comments
 (0)