-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Use BlockCipher.blockSize as a result length in randomIV helper #893
base: main
Are you sure you want to change the base?
Conversation
This should help to avoid two mistakes when `randomIV` helper is used: * Passing of invalid array length for IV generation * Usage of this method for key generation
@krzyzanowskim any feedback for this? |
public extension Cryptors where Self: BlockCipher { | ||
/// Generates array of random bytes. `blockSize` is used as length of the result array. | ||
/// Convenience helper that uses `Swift.SystemRandomNumberGenerator`. | ||
static func randomIV() -> [UInt8] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can it be randomIV(_ count: Int = Self.blockSize)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can, but then it defeats the purpose of these changes. I'd like to make this helper hard to use in a wrong way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not, really. It keep the backward compatibility, while use the blockSize
default now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I've returned randomIV(_ count: Int)
helper marking it as deprecated, so existing code won't be broken after upgrade to the new version
The proposal to use |
Checklist:
Changes proposed in this pull request:
Use
BlockCipher.blockSize
as a result length inrandomIV
helper. This should help to avoid two mistakes whenrandomIV
helper is used:randomIV
helper for a key generation. Because it usesSwift.SystemRandomNumberGenerator
, depending on the platform the result might be not cryptographically secure to be used as a key.Security.SecRandomCopyBytes
should be preferred to generate a key on Apple platforms.