玩转 iOS 开发:探索 CryptoKit 的奥秘
最编程
2024-02-12 13:47:59
...
Apple 在 WWDC2019 推出了一个基于 Swift 的密码框架 CryptoKit,它让生成哈希值、加/解密数据、数字签名和密钥协商变得更加容易。
阅读本文前,需要有一定的密码学基础。请参考之前的一篇文章搞定密码学基础一文。
哈希值
- 提供了 3 种 Hash 函数:
- SHA256
- SHA384
- SHA512
- 哈希普通的 Data 类型的数据
let str = "Hello CryptoKit"
let data = str.data(using: .utf8)!
let hash256 = SHA256.hash(data: data)
let hash384 = SHA384.hash(data: data)
let hash512 = SHA512.hash(data: data)
print(hash256.description)复制代码
- 哈希文件
if let filePath = Bundle.main.path(forResource: "secret", ofType: "json"),
let data = FileManager.default.contents(atPath: filePath) {
let hash256 = SHA256.hash(data: data)
let hash384 = SHA384.hash(data: data)
let hash512 = SHA512.hash(data: data)
print(hash256.description)
}复制代码
HMAC
HMAC 可以理解为一种更安全的 Hash,它需要借助于前面介绍的 Hash 函数。
// 构造一个salt
let salt = "YungFan".data(using: .utf8)!
// 密钥
let key = SymmetricKey(size: .bits256)
// HMAC with SHA256
let authenticationCode = HMAC<SHA256>.authenticationCode(for: salt, using: key)
print(authenticationCode)
// 验证
if HMAC<SHA256>.isValidAuthenticationCode(Data(authenticationCode),
authenticating: salt, using: key) {
print("未被篡改")
}复制代码
加解密数据
支持 AES-GCM 和 ChaChaPoly 算法。开发中首选 ChaChaChaPoly,因为按照官方宣称它在移动设备上的速度更快。ChaChaChaPoly 中的核心概念是ChaChaChaPoly.SealedBox
,它可以理解为只有通过密钥才能访问的数据容器,加密操作时把加密后的密文放在其中,解密操作时需要从中取出密文进行解密。
- 加密
// 明文
let str = "Hello CryptoKit"
let data = str.data(using: .utf8)!
// 密钥,有3种长度的密钥
let key128 = SymmetricKey(size: .bits128)
let key192 = SymmetricKey(size: .bits192)
let key256 = SymmetricKey(size: .bits256)
// 加密
let encryptedContent = try? ChaChaPoly.seal(data, using: key256).combined复制代码
- 解密
// 解密
if let encryptedContent = encryptedContent {
if let sealedBox = try? ChaChaPoly.SealedBox(combined: encryptedContent) {
if let decryptedContent = try? ChaChaPoly.open(sealedBox, using: key256) {
print(String(data: decryptedContent, encoding: .utf8))
}
// SealedBox的3个属性
let nonce = sealedBox.nonce
let ciphertext = sealedBox.ciphertext
let tag = sealedBox.tag
print(sealedBox.combined == nonce + ciphertext + tag)
}
}复制代码
数字签名
内置了 4 种不同的椭圆曲线类型用于创建和验证加密签名,分别是 Curve25519, P521, P384 和 P256,不同的类型有不同的安全性和速度,但通常选择 Curve25519。[563513413](https://jq.qq.com/?_wv=1027&k=lzJejkSl),不管你是大牛还是小白都欢迎入驻
- 产生公/私钥
// 私钥
let privateKey = Curve25519.Signing.PrivateKey()
// 公钥
let publicKey = privateKey.publicKey
// 发布公钥
let publicKeyData = publicKey.rawRepresentation 复制代码
- 私钥签名
let str = "Hello CryptoKit"
let data = str.data(using: .utf8)!
let signature = try? privateKey.signature(for: data)复制代码
- 公钥验证
if let signature = signature {
if publicKey.isValidSignature(NSData(data: signature) , for: data) {
print("签名有效")
}
}复制代码
密钥协商
密钥协商是一个过程,通过这个过程,通信双方可以安全地选择一个加密密钥,然后用它来进行加解密数据。
// 构造一个salt,生成密钥时需要使用
let salt = "YungFan".data(using: .utf8)!
// 用户A和用户B都会生成一对公钥和私钥
let privateKeyA = P521.KeyAgreement.PrivateKey()
let publicKeyA = privateKeyA.publicKey
let privateKeyB = P521.KeyAgreement.PrivateKey()
let publicKeyB = privateKeyB.publicKey
// 用户A用私钥和用户B的公钥产生一个共享的密钥
let sharedSecretA = try? privateKeyA.sharedSecretFromKeyAgreement(with: publicKeyB)
let symmetricKeyA = sharedSecretA?.hkdfDerivedSymmetricKey(using: SHA256.self, salt: salt, sharedInfo: Data(), outputByteCount: 32)
// 用户B用私钥和用户A的公钥产生一个共享的密钥
let sharedSecretB = try? privateKeyB.sharedSecretFromKeyAgreement(with: publicKeyA)
let symmetricKeyB = sharedSecretB?.hkdfDerivedSymmetricKey(using: SHA256.self, salt: salt, sharedInfo: Data(), outputByteCount: 32)
if symmetricKeyA == symmetricKeyB {
print("A和B经过协商产生了共享密钥")
}
上一篇: 3秒快速解密PDF用Python:附带工具下载链接
下一篇: 日常使用的加密与解密技巧方法汇总
推荐阅读
-
Adobe国际认证中文官方网站】Adobe中国摄影计划,免费安装正版激活--Adobe Creative Cloud中国摄影计划。与此同时,Adobe宣布天猫为Adobe Creative Cloud中国摄影计划的电商战略合作伙伴,并将与其合作上线Adobe天猫官方旗舰店。 此举无疑一方面扩大了Adobe在中国的影响力,另一方面也有助于国内用户更好地培养正版软件意识,推动Adobe软件在中国的正版化进程。 网络异常,图片无法显示 ||网络异常 Adobe Creative Cloud中国摄影计划包括Photoshop和Lightroom Classic两大桌面创意工具,以及iOS版Photoshop Express。 其中,Adobe Lightroom Classic和Adobe Photoshop作为两款常用的图像处理软件,对于那些玩摄影、后期修图的创意设计人群无疑有着巨大的帮助,而LR+PS套装对于摄影领域用户的重要性自不必说,正版产品的性能实时更新也可以放心!体验最新功能,对于新镜头(补偿)和机身(RAW 读取)都能第一时间适应。不信你看: Photoshop 图像合成 裁剪、移除对象、润饰合成照片、玩转色彩和特效,创建精美图片和艺术品! Lightroom Classic 照片编辑 轻松批量管理和编辑照片,内置专业创意控件和摄影师预设,让你的照片大放异彩。 手机 PS 便捷编辑 Photoshop Express 支持多种滤镜、贴纸,手机即可完成抠图、除雾等任务 人工智能编辑工具 神经滤镜、快速点击选区、自动选择主题等人工智能功能让图像编辑更轻松 创意画笔内容识别 定制艺术画笔工具,实现个性化效果;内容识别填充,智能去除无用物体。 Adobe Creative Cloud 中国摄影计划的推出,为中国的专业摄影师、摄影爱好者、后期修图和其他创意设计人员带来了全方位的内容和体验。 网络异常,图片无法显示 ||网络异常 当然,不可否认的是,"由于盗版软件缺乏开发、维护和升级成本,销售价格远低于正版软件。再加上很多普通人并不需要使用正版软件的复杂功能,版权观念较淡,还是有大量的创意设计人员会选择盗版软件"。 但事实上,当所有的软件都不再是单一的软件,而是变成一种服务时,单机版盗版的存在就逐渐成为鸡肋。因为有太多的服务让你即使是所谓的 "完美破解",也无法享受,Adobe Cloud 就是一个很好的例子,所谓的完美破解,你只能使用 "Adobe "的一半,对于更精彩的 "云",只能望云兴叹。更何况,越来越多的设计工具从免费走向付费,越来越多的设计师和企业已经接受了付费使用的模式。 其次,对于互联网时代的企业数字化转型而言,数字化合规至关重要。21年来,使用盗版PS和未经授权的方正字体被指侵权的事情闹得沸沸扬扬,虽然新闻真假难辨,但也给使用盗版工具的用户敲响了警钟。 付费使用正版工具,可以更放心地进行设计,不用担心版权风险!
-
玩转C++:探索位图、布隆过滤器与哈希分割的奥秘
-
玩转C/C++基础教程:2.理解输入与输出(🌟推荐新手阅读) - 5.探索转义字符的奥秘
-
搞定Java分布式开发!一起探索Kafka消息队列的奥秘
-
玩转数据结构:探索双链表的奥秘
-
玩转 iOS 开发:探索 CryptoKit 的奥秘
-
iOS应用开发实战:探索工程化过程中的三层结构 - MVC与MVVM模式详解
-
玩转图片处理:探索色彩空间的奥秘
-
玩转Python编程:探索Paramiko模块的奥秘
-
玩转Python编程:探索Paramiko模块的奥秘