Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unsafe Swift code from OTP code generation #105

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Guardian/Generators/OneTimePasswordGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@
let buffer = Data(bytes: &c, count: MemoryLayout<UInt64>.size);
let digestData = hmac.sign(buffer)
let length = MemoryLayout<UInt32>.size
// digestData.count - 1 will always be >0, because digestData depends on algorythm and can be only 20, 32, or 64.
let offset = Int(digestData[digestData.count - 1] & 0x0f)
// offset is always <=15, length is always 4, so prefix will always be within digestData count.
guard let offset = digestData.last.map({ Int($0 & 0x0f) }),
(offset + length) < digestData.count else {
return 0

Check warning on line 88 in Guardian/Generators/OneTimePasswordGenerator.swift

View check run for this annotation

Codecov / codecov/patch

Guardian/Generators/OneTimePasswordGenerator.swift#L88

Added line #L88 was not covered by tests
}
var hash = digestData.dropFirst(offset).prefix(length).reduce(0, { $0 << 8 | UInt32($1) })
hash &= 0x7fffffff
hash = hash % UInt32(pow(10, Float(self.parameters.digits)))
Expand Down
Loading