swift - Password hashing using CryptoKit
问题描述
I'm using (CryptoKit) to use AES-GCM to encrypt some data and authenticate it as well.
However, I was wondering how I would generate an AES-GCM key from a plain text password. Normally, you would use a KDF
function for that, like PBKDF2.
In CryptoKit
, there is a HKDF
class which does about what I want: https://developer.apple.com/documentation/cryptokit/hkdf
However, I am wondering what KDF algorithm the DeriveKey function uses. Does it use PBKDF2? Does it use bcrypt
? If so, how do I specify settings, or are the settings automatically determined?
解决方案
HKDF is defined in RFC5869. It is intended to generate keys from some cryptographically secure "keying material" (IKM). It is not intended for stretching a human-generated password. As discussed in section 4 Applications of HKDF:
On the other hand, it is anticipated that some applications will not be able to use HKDF "as-is" due to specific operational requirements, or will be able to use it but without the full benefits of the scheme. One significant example is the derivation of cryptographic keys from a source of low entropy, such as a user's password. The extract step in HKDF can concentrate existing entropy but cannot amplify entropy. In the case of password-based KDFs, a main goal is to slow down dictionary attacks using two ingredients: a salt value, and the intentional slowing of the key derivation computation. HKDF naturally accommodates the use of salt; however, a slowing down mechanism is not part of this specification. Applications interested in a password-based KDF should consider whether, for example, [PKCS5] meets their needs better than HKDF.
I don't believe that CryptoKit offers a PBKDF of any kind (PBKDF2, scrypt, bcrypt, argon2). It's a very limited framework (I have yet to find a situation where it was useful). You will likely need to continue to use CommonCrypto for this, or implement it yourself (or use something like CryptoSwift, which I believe implements several).
推荐阅读
- visual-studio - 是否可以导航到 VS 2017 中收集在任意文本文件而不是错误列表窗格中的 msbuild 错误?
- php - 给定 PHP 和 SQL 中的以下结构,我如何返回下一个可用的约会?
- microsoft-cognitive - 是否可以将照片中的人脸添加到没有 Face ID 的人?
- rust - 如何使用更高等级的特征边界使返回的 impl Fn 更通用?
- kendo-ui - Kendo UI 网格编辑器字段自定义验证
- c# - 如何发送 mailmessage.to 使收件人看不到彼此的电子邮件
- hive - 计算值之间的最大差异并记录两个值在 HIVE 中发生的时间
- c# - mscorlib 的 `System.Boolean` 如何避免结构布局循环?
- python - 获取要在 entry_field 小部件中填充的文件路径
- microcontroller - IR遥控接口PIC16f877a