zoukankan      html  css  js  c++  java
  • swift 获取文件的Md5值

    获取文件的Md5值的方法如下

    func md5File(url: URL) -> String? {
            
            let bufferSize = 1024 * 1024
            
            do {
                //打开文件
                let file = try FileHandle(forReadingFrom: url)
                defer {
                    file.closeFile()
                }
                
                //初始化内容
                var context = CC_MD5_CTX()
                CC_MD5_Init(&context)
                
                //读取文件信息
                while case let data = file.readData(ofLength: bufferSize), data.count > 0 {
                    data.withUnsafeBytes {
                        _ = CC_MD5_Update(&context, $0, CC_LONG(data.count))
                    }
                }
                
                //计算Md5摘要
                var digest = Data(count: Int(CC_MD5_DIGEST_LENGTH))
                digest.withUnsafeMutableBytes {
                    _ = CC_MD5_Final($0, &context)
                }
                
                return digest.map { String(format: "%02hhx", $0) }.joined()
                
            } catch {
                print("Cannot open file:", error.localizedDescription)
                return nil
            }
        }
    

    不合理的地方请指正 备份:http://www.jianshu.com/p/28f4f34a21d9

  • 相关阅读:
    Educational Codeforces Round 23E
    Educational Codeforces Round 23D
    Codeforces Round #461 (Div. 2)
    HYSBZ
    HDU
    HYSBZ
    HYSBZ
    SPOJ
    点击搜索条件提交form表单
    HTML颜色获取工具,colorpicker
  • 原文地址:https://www.cnblogs.com/kunH/p/7234915.html
Copyright © 2011-2022 走看看