News for hackers. Stuff that matters.
Re:Swift System Is Now Open Source by ChrisMarshallNY on Friday September 25, 2020 @03:41PM (#24593391)
Yes, and I'm glad for it. Not sure if it's "ready for prime time," yet (it sounds like it may still be too "nascent" for me), but I'll have to see if I can use it to tweak stuff like this[0]:

    var md5: String {
        // The reason we are declaring these here, is so we don't have to actally import the CC module. We will just grope around and find the entry point, ourselves.
        
        /// This is a cast for [the MD5 function](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/CC_MD5.3cc.html#//apple_ref/doc/man/3cc/CC_MD5). [The convention attribute](https://docs.swift.org/swift-book/ReferenceManual/Attributes.html#ID600) just says that it's a "raw" C function.
        typealias CC_MD5_TYPE = @convention(c) (UnsafeRawPointer, UInt32, UnsafeMutableRawPointer) -> UnsafeMutableRawPointer
        
        // This is a flag, telling the name lookup to happen in the global scope. No dlopen required.
        let RTLD_DEFAULT = UnsafeMutableRawPointer(bitPattern: -2)
        
        // This loads a function pointer with the CommonCrypto MD5 function.
        // [dlsym](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dlsym.3.html) is a symbol lookup. It finds the symbol in our library, and returns a pointer to it.
        let CC_MD5 = unsafeBitCast(dlsym(RTLD_DEFAULT, "CC_MD5")!, to: CC_MD5_TYPE.self)
        
        // This is the length of the hash
        let CC_MD5_DIGEST_LENGTH = 16
    
        guard let strData = self.data(using: .utf8) else { return "" }
        
        /// Creates an array of unsigned 8 bit integers that contains 16 zeros
        var digest = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))

        /// CC_MD5 performs digest calculation and places the result in the caller-supplied buffer for digest (md)
        /// Calls the given closure with a pointer to the underlying unsafe bytes of the strData’s contiguous storage.
        _ = strData.withUnsafeBytes { (inBytes) -> Int in
            // CommonCrypto
            // extern unsigned char *CC_MD5(const void *data, CC_LONG len, unsigned char *md) --|
            // OpenSSL                                                                          |
            // unsigned char *MD5(const unsigned char *d, size_t n, unsigned char *md)        <-|
            if let baseAddr = inBytes.baseAddress {
                _ = CC_MD5(baseAddr, UInt32(strData.count), &digest)
            }

            return 0
        }
        
        // Convert the numerical response to an uppercase hex string.
        return digest.reduce("") { (current, new) -> String in String(format: "\(current)%02X", new) }
    }
As a Swift programmer, the above hurts my heart (but it works very well).

[0] https://github.com/RiftValleySoftware/RVS_Generic_Swift_Tool...

Re:Swift System Is Now Open Source by saagarjha on Friday September 25, 2020 @07:24PM (#24595067)
You know you can just call the function directly, right? (Stupid warnings about MD5 being insecure aside…)
Re:Swift System Is Now Open Source by ChrisMarshallNY on Friday September 25, 2020 @09:06PM (#24595584)
Didn’t want to import CC.

This is one of a couple of computed CC functions in a very general-purpose StringProtocol extension. I wanted to reduce as many external requirements as possible. I don’t even like importing Foundation, if I can help it.

Re:Swift System Is Now Open Source by djxfade on Friday September 25, 2020 @06:12PM (#24594647)
import Foundation import CryptoKit

extension String { var md5: String { let computed = Insecure.MD5.hash(data: self.data(using: .utf8)!) return computed.map { String(format: "%02hhx", $0) }.joined() } }