In Swift 2, I used the following code to extend string variables and to be able to make sha1, sha256, and md5.
After moving to swift 3, the code is not working any more! I tried to convert it but run into continuous errors.
Any idea how can I solve this?
extension NSData {
func hexString() -> String {
var string = String()
for i in UnsafeBufferPointer<UInt8>(start: UnsafeMutablePointer<UInt8>(bytes), count: length) {
string += Int(i).hexString()
}
return string
}
func MD5() -> NSData {
let result = NSMutableData(length: Int(CC_MD5_DIGEST_LENGTH))!
CC_MD5(bytes, CC_LONG(length), UnsafeMutablePointer<UInt8>(result.mutableBytes))
return NSData(data: result)
}
func SHA1() -> NSData {
let result = NSMutableData(length: Int(CC_SHA1_DIGEST_LENGTH))!
CC_SHA1(bytes, CC_LONG(length), UnsafeMutablePointer<UInt8>(result.mutableBytes))
return NSData(data: result)
}
func SHA256() -> NSData {
let result = NSMutableData(length: Int(CC_SHA256_DIGEST_LENGTH))
CC_SHA256(bytes, CC_LONG(length), UnsafeMutablePointer(result!.mutableBytes))
return NSData(data: result!)
}
}
extension String {
func hexString() -> String {
return (self as NSString).dataUsingEncoding(NSUTF8StringEncoding)!.hexString()
}
func MD5() -> String {
return (self as NSString).dataUsingEncoding(NSUTF8StringEncoding)!.MD5().hexString()
}
func SHA1() -> String {
return (self as NSString).dataUsingEncoding(NSUTF8StringEncoding)!.SHA1().hexString()
}
func SHA256() -> String {
return (self as NSString).dataUsingEncoding(NSUTF8StringEncoding)!.SHA256().hexString()
}
}
You'd better use Swift Data
in Swift 3.
Data
And when working with Data
, you need to use withUnsafeBytes(_:)
or withUnsafeMutableBytes(_:)
, where you were using bytes
or mutableBytes
respectively.
withUnsafeBytes(_:)
withUnsafeMutableBytes(_:)
extension Data {
func hexString() -> String {
let string = self.map{Int($0).hexString()}.joined()
return string
}
func MD5() -> Data {
var result = Data(count: Int(CC_MD5_DIGEST_LENGTH))
_ = result.withUnsafeMutableBytes {resultPtr in
self.withUnsafeBytes {(bytes: UnsafePointer<UInt8>) in
CC_MD5(bytes, CC_LONG(count), resultPtr)
}
}
return result
}
/*
... nearly the same for `SHA1` and `SHA256`.
*/
}
extension String {
func hexString() -> String {
return self.data(using: .utf8)!.hexString()
}
func MD5() -> String {
return self.data(using: .utf8)!.MD5().hexString()
}
/*
... nearly the same for `SHA1` and `SHA256`.
*/
}
I prefer making computed properties than no-argument methods (for relatively light-tasks). You need to fix all parts using them, but you can write something like this:
extension Int {
var hexString: String {
return ...
}
}
extension Data {
var hexString: String {
let string = self.map{Int($0).hexString}.joined()
return string
}
var MD5: Data {
var result = Data(count: Int(CC_MD5_DIGEST_LENGTH))
_ = result.withUnsafeMutableBytes {resultPtr in
self.withUnsafeBytes {(bytes: UnsafePointer<UInt8>) in
CC_MD5(bytes, CC_LONG(count), resultPtr)
}
}
return result
}
/*
... nearly the same for `SHA1` and `SHA256`.
*/
}
extension String {
var hexString: String {
return self.data(using: .utf8)!.hexString
}
var MD5: String {
return self.data(using: .utf8)!.MD5.hexString
}
/*
... nearly the same for `SHA1` and `SHA256`.
*/
}
There may be a quicker fix for your code using NSData
, but I recommend you to move to Data
in Swift 3.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With