Skip to content

Commit

Permalink
Merge pull request #159 from theleftbit/swift-crypto
Browse files Browse the repository at this point in the history
Migrate to Crypto Swift
  • Loading branch information
piercifani committed Jan 8, 2025
2 parents d62386e + ffa7f92 commit 1c54bac
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/buildEmerge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
jobs:
build:
name: Build XCFramework
runs-on: macos-latest
runs-on: ios
env:
PRODUCT_NAME: BSWFoundation
steps:
Expand Down
43 changes: 30 additions & 13 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
{
"object": {
"pins": [
{
"package": "KeychainAccess",
"repositoryURL": "https://github.com/kishikawakatsumi/KeychainAccess.git",
"state": {
"branch": null,
"revision": "84e546727d66f1adc5439debad16270d0fdd04e7",
"version": "4.2.2"
}
"originHash" : "5b50206e1531128786d1b20d811cf2cf4dc8d6626d754cefcf682abcd03b3a9a",
"pins" : [
{
"identity" : "keychainaccess",
"kind" : "remoteSourceControl",
"location" : "https://github.com/kishikawakatsumi/KeychainAccess.git",
"state" : {
"revision" : "84e546727d66f1adc5439debad16270d0fdd04e7",
"version" : "4.2.2"
}
]
},
"version": 1
},
{
"identity" : "swift-asn1",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-asn1.git",
"state" : {
"revision" : "7faebca1ea4f9aaf0cda1cef7c43aecd2311ddf6",
"version" : "1.3.0"
}
},
{
"identity" : "swift-crypto",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-crypto.git",
"state" : {
"revision" : "ff0f781cf7c6a22d52957e50b104f5768b50c779",
"version" : "3.10.0"
}
}
],
"version" : 3
}
6 changes: 5 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/kishikawakatsumi/KeychainAccess.git", from: "4.2.2"),
.package(url: "https://github.com/apple/swift-crypto.git", from: "3.10.0"),
],
targets: [
.target(
name: "BSWFoundation",
dependencies: ["KeychainAccess"]
dependencies: [
.product(name: "Crypto", package: "swift-crypto"),
.product(name: "KeychainAccess", package: "KeychainAccess"),
]
),
.testTarget(
name: "BSWFoundationTests",
Expand Down
77 changes: 29 additions & 48 deletions Sources/BSWFoundation/Extensions/String+Crypto.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,45 @@
// Created by Pierluigi Cifani on 13/06/2019.
//


import Crypto
import Foundation
import CommonCrypto

public extension String {

func hmac(algorithm: CryptoAlgorithm, key: String) -> String {
let str = self.cString(using: String.Encoding.utf8)
let strLen = Int(self.lengthOfBytes(using: String.Encoding.utf8))
let digestLen = algorithm.digestLength
let result = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: digestLen)
let keyStr = key.cString(using: String.Encoding.utf8)
let keyLen = Int(key.lengthOfBytes(using: String.Encoding.utf8))

CCHmac(algorithm.HMACAlgorithm, keyStr!, keyLen, str!, strLen, result)

let digest = stringFromResult(result: result, length: digestLen)

result.deallocate()
let keyData = Data(key.utf8)
let messageData = Data(self.utf8)
let key = SymmetricKey(data: keyData)
let hmac: Data
switch algorithm {
case .SHA256:
hmac = Data(HMAC<SHA256>
.authenticationCode(for: messageData, using: key))
case .SHA384:
hmac = Data(HMAC<SHA384>
.authenticationCode(for: messageData, using: key))
case .SHA512:
hmac = Data(HMAC<SHA512>
.authenticationCode(for: messageData, using: key))
}

return digest
return stringFromResult(result: [UInt8](hmac), length: algorithm.digestLength)
}

private func stringFromResult(result: UnsafeMutablePointer<CUnsignedChar>, length: Int) -> String {
let hash = NSMutableString()
for i in 0..<length {
hash.appendFormat("%02x", result[i])
}
return String(hash).lowercased()
private func stringFromResult(result: [UInt8], length: Int) -> String {
result.prefix(length).map { String(format: "%02x", $0) }.joined()
}
}

public enum CryptoAlgorithm {
case MD5, SHA1, SHA224, SHA256, SHA384, SHA512

fileprivate var HMACAlgorithm: CCHmacAlgorithm {
var result: Int = 0
switch self {
case .MD5: result = kCCHmacAlgMD5
case .SHA1: result = kCCHmacAlgSHA1
case .SHA224: result = kCCHmacAlgSHA224
case .SHA256: result = kCCHmacAlgSHA256
case .SHA384: result = kCCHmacAlgSHA384
case .SHA512: result = kCCHmacAlgSHA512
}
return CCHmacAlgorithm(result)
}

fileprivate var digestLength: Int {
var result: Int32 = 0
switch self {
case .MD5: result = CC_MD5_DIGEST_LENGTH
case .SHA1: result = CC_SHA1_DIGEST_LENGTH
case .SHA224: result = CC_SHA224_DIGEST_LENGTH
case .SHA256: result = CC_SHA256_DIGEST_LENGTH
case .SHA384: result = CC_SHA384_DIGEST_LENGTH
case .SHA512: result = CC_SHA512_DIGEST_LENGTH
enum CryptoAlgorithm {
case SHA256, SHA384, SHA512

fileprivate var digestLength: Int {
switch self {
case .SHA256: return 32
case .SHA384: return 48
case .SHA512: return 64
}
}
return Int(result)
}
}

0 comments on commit 1c54bac

Please sign in to comment.