jamiechoo

 

swift原生的SHA-256全部功能和实例

Swift 通过 CryptoKit 提供了原生的 SHA-256 功能(以及其他加密算法)。CryptoKit 是苹果公司提供的现代加密库,可以用于计算散列(哈希)值、加密、解密和其他密码学相关操作。它支持的功能包括 SHA-256、SHA-384、SHA-512、HMAC 以及对称和非对称加密等。

以下是 Swift 中与 SHA-256 相关的功能,以及它们的示例。

1. SHA-256

计算给定数据的 SHA-256 哈希值。

示例:

import CryptoKit
 
let input = "Hello, world!".data(using: .utf8)!
let hash = SHA256.hash(data: input) // 将哈希值转换为十六进制字符串
let hashString = hash.compactMap { String(format: "%02x", $0) }.joined()
print(hashString)
// 输出:315f5bdb76d078c43b8ac0064e4a016461363ddc2a179ae6c1f5849d196637c1

2. SHA-384

计算给定数据的 SHA-384 哈希值。

示例:

import CryptoKit
 
let input = "Hello, world!".data(using: .utf8)!
let hash = SHA384.hash(data: input)
let hashString = hash.compactMap { String(format: "%02x", $0) }.joined()
print(hashString) // 输出较长的 SHA-384 哈希值

3. SHA-512

计算给定数据的 SHA-512 哈希值。

示例:

import CryptoKit
 
let input = "Hello, world!".data(using: .utf8)!
let hash = SHA512.hash(data: input)
let hashString = hash.compactMap { String(format: "%02x", $0) }.joined()
print(hashString) // 输出较长的 SHA-512 哈希值

4. HMAC with SHA-256

使用 HMAC-SHA-256 来进行基于密钥的消息验证码 (HMAC) 运算。

示例:

import CryptoKit
 
let key = SymmetricKey(size: .bits256)
let input = "Hello, world!".data(using: .utf8)!
let hmac = HMAC<SHA256>.authenticationCode(for: input, using: key)
let hmacString = hmac.compactMap { String(format: "%02x", $0) }.joined()
print(hmacString) // 输出 HMAC-SHA-256 值

5. Incremental Hashing (SHA-256)

增量式地计算 SHA-256 哈希值,比如用于处理大量数据流。

示例:

import CryptoKit
 
var hasher = SHA256() // 分批添加数据
let part1 = "Hello, ".data(using: .utf8)!
let part2 = "world!".data(using: .utf8)!
hasher.update(data: part1)
hasher.update(data: part2) // 完成哈希计算
let hash = hasher.finalize()
let hashString = hash.compactMap { String(format: "%02x", $0) }.joined()
print(hashString) // 输出最终的哈希值

6. Hashing Strings Using SHA256

使用 SHA256 计算字符串的哈希值。

示例:

import CryptoKit
 
let input = "Hello, world!"
let inputData = Data(input.utf8)
let hash = SHA256.hash(data: inputData)
let hashString = hash.map { String(format: "%02hhx", $0) }.joined()
print(hashString) // 输出 SHA-256 哈希值

7. Verifying HMAC with SHA-256

验证 HMAC-SHA-256 生成的签名。

示例:

import CryptoKit
 
let key = SymmetricKey(size: .bits256)
let input = "Hello, world!".data(using: .utf8)!
let hmac = HMAC<SHA256>.authenticationCode(for: input, using: key) // 验证 HMAC 是否匹配
let isValid = HMAC<SHA256>.isValidAuthenticationCode(hmac, authenticating: input, using: key)
print(isValid) // 输出:true

8. Comparing Hash Values

比较两个哈希值是否相等。

示例:

import CryptoKit
 
let input1 = "Hello, world!".data(using: .utf8)!
let input2 = "Hello, Crypto!".data(using: .utf8)!
let hash1 = SHA256.hash(data: input1)
let hash2 = SHA256.hash(data: input2)
 
  if hash1 == hash2 {
    print("Hashes are equal")
    } else {
    print("Hashes are different")
  }

总结

  • SHA256:用于计算 256 位哈希值。
  • SHA384SHA512:分别用于计算 384 位和 512 位哈希值。
  • HMAC:用于基于密钥的哈希消息验证,确保消息的完整性和真实性。
  • 增量哈希:用于大数据分批处理的哈希计算。

通过 CryptoKit,你可以轻松地在 Swift 中使用现代加密技术,处理哈希和加密相关的任务。

posted on 2024-10-03 16:49  jamiechoo  阅读(218)  评论(0)    收藏  举报

导航