gpt4 book ai didi

swift3 - Swift 3 制作 sha1、sha256 和 md5 函数

转载 作者:行者123 更新时间:2023-12-04 03:14:40 24 4
gpt4 key购买 nike

在 Swift 2 中,我使用以下代码扩展字符串变量并能够生成 sha1、sha256 和 md5。

移至 swift 3 后,代码不再起作用!我试图转换它,但遇到连续错误。

知道我该如何解决这个问题吗?

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()
}

}

最佳答案

你最好用 Swift Data在 swift 3 中。

Data

当与 Data 一起工作时,您需要使用 withUnsafeBytes(_:)withUnsafeMutableBytes(_:) ,您在何处使用 bytesmutableBytes分别。

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`.
*/
}

我更喜欢计算属性而不是无参数方法(对于相对较轻的任务)。您需要使用它们修复所有部分,但您可以编写如下内容:
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`.
*/
}

使用 NSData 可能会更快地修复您的代码,但我建议您移至 Data在 swift 3 中。

关于swift3 - Swift 3 制作 sha1、sha256 和 md5 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39684092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com