xcode 如何从 SWIFT 中的字符串获取 MD5 哈希并制作桥头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31964291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:39:51  来源:igfitidea点击:

How to get MD5 hash from string in SWIFT and make bridge-header

xcodeswifthashmd5

提问by Mike

i dont even expect this problem, but it appears. I try to get md5 hash from string in swift. I search about that on SO and assume that i need to import library like that:

我什至不希望有这个问题,但它出现了。我尝试快速从字符串中获取 md5 哈希值。我在 SO 上搜索并假设我需要像这样导入库:

#import <CommonCrypto/CommonCrypto.h>

First of all compiler said that '#' is not okay. Then i removed and compiler said that '<' is not okay. I tried to figure out that and find recommendations to add folder named "CommonCrypto" and create a file named "module.map". I cant understand how to create file with this extension. Okay, i create swift file and replace its extension. Then write code there:

首先编译器说'#'不行。然后我删除了,编译器说 '<' 不好。我试图弄清楚并找到添加名为“CommonCrypto”的文件夹并创建一个名为“module.map”的文件的建议。我无法理解如何使用此扩展名创建文件。好的,我创建了 swift 文件并替换了它的扩展名。然后在那里写代码:

module CommonCrypto [system] {
    header "/usr/include/CommonCrypto/CommonCrypto.h"
    export *
}

and again its not okay Then in recommendations was adding the new module to Import Paths under Swift Compiler – Search Paths in your project settings ${SRCROOT}/CommonCrypto).

再次它不行然后在建议中将新模块添加到 Swift 编译器下的导入路径 - 在您的项目设置 ${SRCROOT}/CommonCrypto 中搜索路径)。

and its again not okay.

它又不好。

i cant belive that its so difficult to do that. i think i misunderstand some steps or something. if you know step by step answer please help))

我无法相信它是如此困难做到这一点。我想我误解了一些步骤或某事。如果你知道一步一步的答案,请帮忙))

回答by zaph

You need to add a bridging header and add the #import <CommonCrypto/CommonCrypto.h>statement to it.

您需要添加一个桥接头并向其添加#import <CommonCrypto/CommonCrypto.h>语句。

The easiest way to add a bridging header is to add an Objective-C file to the project, you will be aked ig you want to add a bridging header, reply yes. After that you can delete the Objective-C file file that was added.

添加桥接头最简单的方法是在项目中添加一个Objective-C文件,你会被问到要添加桥接头,回复yes。之后,您可以删除添加的 Objective-C 文件文件。

Example code:

示例代码:

func md5(#string: String) -> NSData {
    var digest = NSMutableData(length: Int(CC_MD5_DIGEST_LENGTH))!
    if let data :NSData = string.dataUsingEncoding(NSUTF8StringEncoding) {
        CC_MD5(data.bytes, CC_LONG(data.length),
            UnsafeMutablePointer<UInt8>(digest.mutableBytes))
    }
    return digest
}

//Test:
let digest = md5(string:"Here is the test string")
println("digest: \(digest)")

Output:

输出:

digest: 8f833933 03a151ea 33bf6e3e bbc28594

摘要:8f833933 03a151ea 33bf6e3e bbc28594

Here is a more Swift 2.0 version returning an array of UInt8:

这是一个更 Swift 2.0 版本,返回一个数组UInt8

func md5(string string: String) -> [UInt8] {
    var digest = [UInt8](count: Int(CC_MD5_DIGEST_LENGTH), repeatedValue: 0)
    if let data = string.dataUsingEncoding(NSUTF8StringEncoding) {
        CC_MD5(data.bytes, CC_LONG(data.length), &digest)
    }

    return digest
}

回答by jjrscott

A solution for Swift 4.1:

Swift 4.1 的解决方案:

import CommonCrypto

extension Data
{
    func md5() -> Data
    {
        var digest = Data(count: Int(CC_MD5_DIGEST_LENGTH))

        self.withUnsafeBytes { (bytes : UnsafePointer<UInt8>) -> Void in
            digest.withUnsafeMutableBytes { (mutableBytes : UnsafeMutablePointer<UInt8>) -> Void in
                CC_MD5(bytes, CC_LONG(self.count), mutableBytes)
            }
        }

        return digest
    }
}

See Importing CommonCrypto in a Swift frameworkfor the CommonCryptopart.

在斯威夫特框架导入CommonCryptoCommonCrypto一部分。