Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
onmyway133 committed Jan 24, 2021
1 parent 29e6a20 commit 3024814
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
42 changes: 21 additions & 21 deletions Sources/Arcane/HMAC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,80 +10,80 @@ import Foundation
import CommonCrypto

public struct HMAC {

public enum DigestType {
case hex
case base64
}

// MARK: - NSData

public static func MD5(_ data: Data, key: Data) -> Data? {
return HMAC.generate(data, key: key, crypto: .MD5)
}

public static func SHA1(_ data: Data, key: Data) -> Data? {
return HMAC.generate(data, key: key, crypto: .SHA1)
}

public static func SHA224(_ data: Data, key: Data) -> Data? {
return HMAC.generate(data, key: key, crypto: .SHA224)
}

public static func SHA256(_ data: Data, key: Data) -> Data? {
return HMAC.generate(data, key: key, crypto: .SHA256)
}

public static func SHA384(_ data: Data, key: Data) -> Data? {
return HMAC.generate(data, key: key, crypto: .SHA384)
}

public static func SHA512(_ data: Data, key: Data) -> Data? {
return HMAC.generate(data, key: key, crypto: .SHA512)
}

static func generate(_ data: Data, key: Data, crypto: Crypto) -> Data? {
guard let HMACAlgorithm = crypto.HMACAlgorithm else { return nil }

// Can also use UnsafeMutablePointer<CUnsignedChar>.alloc(Int(crypto.length))
var buffer = Array<UInt8>(repeating: 0, count: Int(crypto.length))
CCHmac(HMACAlgorithm, (key as NSData).bytes, key.count, (data as NSData).bytes, data.count, &buffer)
return Data(bytes: UnsafePointer<UInt8>(buffer), count: Int(crypto.length))

return Data(buffer)
}

// MARK: - String

public static func MD5(_ string: String, key: String, digest: DigestType = .hex) -> String? {
return HMAC.generate(string, key: key, crypto: .MD5, digest: digest)
}

public static func SHA1(_ string: String, key: String, digest: DigestType = .hex) -> String? {
return HMAC.generate(string, key: key, crypto: .SHA1, digest: digest)
}

public static func SHA224(_ string: String, key: String, digest: DigestType = .hex) -> String? {
return HMAC.generate(string, key: key, crypto: .SHA224, digest: digest)
}

public static func SHA256(_ string: String, key: String, digest: DigestType = .hex) -> String? {
return HMAC.generate(string, key: key, crypto: .SHA256, digest: digest)
}

public static func SHA384(_ string: String, key: String, digest: DigestType = .hex) -> String? {
return HMAC.generate(string, key: key, crypto: .SHA384, digest: digest)
}

public static func SHA512(_ string: String, key: String, digest: DigestType = .hex) -> String? {
return HMAC.generate(string, key: key, crypto: .SHA512, digest: digest)
}

static func generate(_ string: String, key: String, crypto: Crypto, digest: DigestType) -> String? {
guard let data = string.data(using: String.Encoding.utf8),
let keyData = key.data(using: String.Encoding.utf8),
let generatedData = HMAC.generate(data, key: keyData, crypto: crypto)
else { return nil }

return digest == .hex ? generatedData.hexString : generatedData.base64String
}
}
4 changes: 2 additions & 2 deletions Sources/Arcane/Hash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public struct Hash {
static func hash(_ data: Data, crypto: Crypto) -> Data {
var buffer = Array<UInt8>(repeating: 0, count: Int(crypto.length))
crypto.method(data: (data as NSData).bytes, length: UInt32(data.count), buffer: &buffer)
return Data(bytes: UnsafePointer<UInt8>(buffer), count: buffer.count)

return Data(buffer)
}

// MARK: - String
Expand Down

0 comments on commit 3024814

Please sign in to comment.