Created
February 28, 2020 03:32
-
-
Save dogo/7c4235d2398a5513574c58cd95bc4345 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import CommonCrypto | |
extension Data { | |
/// Hashing algorithm that prepends an RSA2048ASN1Header to the beginning of the data being hashed. | |
/// | |
/// - Parameters: | |
/// - type: The type of hash algorithm to use for the hashing operation. | |
/// - output: The type of output string desired. | |
/// - Returns: A hash string using the specified hashing algorithm, or nil. | |
public func hashWithRSA2048Asn1Header(_ type: HashType, output: HashOutputType = .hex) -> String? { | |
let rsa2048Asn1Header:[UInt8] = [ | |
0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, | |
0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00 | |
] | |
var headerData = Data(rsa2048Asn1Header) | |
headerData.append(self) | |
return hashed(type, output: output) | |
} | |
/// Hashing algorithm for hashing a Data instance. | |
/// | |
/// - Parameters: | |
/// - type: The type of hash to use. | |
/// - output: The type of hash output desired, defaults to .hex. | |
/// - Returns: The requested hash output or nil if failure. | |
public func hashed(_ type: HashType, output: HashOutputType = .hex) -> String? { | |
// setup data variable to hold hashed value | |
var digest = Data(count: Int(type.length)) | |
_ = digest.withUnsafeMutableBytes{ digestBytes -> UInt8 in | |
self.withUnsafeBytes { messageBytes -> UInt8 in | |
if let mb = messageBytes.baseAddress, let db = digestBytes.bindMemory(to: UInt8.self).baseAddress { | |
let length = CC_LONG(self.count) | |
switch type { | |
case .md5: CC_MD5(mb, length, db) | |
case .sha1: CC_SHA1(mb, length, db) | |
case .sha224: CC_SHA224(mb, length, db) | |
case .sha256: CC_SHA256(mb, length, db) | |
case .sha384: CC_SHA384(mb, length, db) | |
case .sha512: CC_SHA512(mb, length, db) | |
} | |
} | |
return 0 | |
} | |
} | |
// return the value based on the specified output type. | |
switch output { | |
case .hex: return digest.map { String(format: "%02hhx", $0) }.joined() | |
case .base64: return digest.base64EncodedString() | |
} | |
} | |
} |
Author
dogo
commented
Feb 28, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment