SwiftyRSA
SwiftyRSA copied to clipboard
Translating NSData+SHA.m to swift file
Hi,
it's not really an issue, more an optimisation, I am trying to keep as less as possible obj-c code since it hits app launch time and I wonder, if there is any swift limitation that prevents to translate NSData+SHA.m to swift file?
Unfortunately I am too weak in programming and I didn't get there completely:
import CommonCrypto import Foundation
func swiftyRSASHA1() -> Data {
let outputLength = CC_SHA1_DIGEST_LENGTH
let output = [UInt8](repeating: 0, count: Int(outputLength))
CC_SHA1(bytes, CC_LONG(UInt(count)), output)
return Data(bytes: output, count: Int(outputLength))
}
I get
Cannot_ convert value of type '[UInt8]' to expected argument type 'UnsafeMutablePointer<UInt8>?
for CC_SHA1(bytes, CC_LONG(UInt(count)), output)
Thank you!
public struct SHA {
static func sha(_ data: Data, type: Signature.DigestType) -> Data {
switch type {
case .sha1:
return sha1(data)
case .sha224:
return sha224(data)
case .sha256:
return sha256(data)
case .sha384:
return sha384(data)
case .sha512:
return sha512(data)
}
}
static func sha1(_ data: Data) -> Data {
var digest = [UInt8](repeating: 0, count: Int(CC_SHA1_DIGEST_LENGTH))
data.withUnsafeBytes {
_ = CC_SHA1($0.baseAddress, CC_LONG(data.count), &digest)
}
return Data(bytes: digest, count: digest.count)
}
static func sha224(_ data: Data) -> Data {
var digest = [UInt8](repeating: 0, count: Int(CC_SHA224_DIGEST_LENGTH))
data.withUnsafeBytes {
_ = CC_SHA224($0.baseAddress, CC_LONG(data.count), &digest)
}
return Data(bytes: digest, count: digest.count)
}
static func sha256(_ data: Data) -> Data {
var digest = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
data.withUnsafeBytes {
_ = CC_SHA256($0.baseAddress, CC_LONG(data.count), &digest)
}
return Data(bytes: digest, count: digest.count)
}
static func sha384(_ data: Data) -> Data {
var digest = [UInt8](repeating: 0, count: Int(CC_SHA384_DIGEST_LENGTH))
data.withUnsafeBytes {
_ = CC_SHA384($0.baseAddress, CC_LONG(data.count), &digest)
}
return Data(bytes: digest, count: digest.count)
}
static func sha512(_ data: Data) -> Data {
var digest = [UInt8](repeating: 0, count: Int(CC_SHA512_DIGEST_LENGTH))
data.withUnsafeBytes {
_ = CC_SHA512($0.baseAddress, CC_LONG(data.count), &digest)
}
return Data(bytes: digest, count: digest.count)
}
}