bitmark-inc / tweetnacl-swiftwrap

MIT License
20 stars 23 forks source link

Please make the functions from NaclUtil public #8

Closed ppamorim closed 3 years ago

ppamorim commented 3 years ago

Hi. I am using this project in my cocoapods library and I need to use some functions from NaclUtil that are private. Could you please make struct NaclUtil public? Otherwise I will need to manually reimplement them in my source code.

anhnguyenbitmark commented 3 years ago

Which functions do you need to expose from NaclUtil? It was initially served for other methods, so I didn't make it public.

ppamorim commented 3 years ago

Mostly this function: https://github.com/ppamorim/skynet-swift/blob/master/Sources/Skynet/Model/SkynetUser.swift#L202

danielrbrowne commented 3 years ago

I'd also like to see this change, please. A note, however is that a small change to its implementation means that the library can be used on Linux where Apple's Security.framework isn't available. Currently I reimplemented the method as:

/// Generates cryptographically secure random data.
/// - Parameter count: The number of random bytes to return.
/// - Throws: An `CryptoError` if the random data could not be generated for some reason,
///           or if zero random bytes were requested.
/// - Returns: The requested number of cryptographically secure random bytes, as `Data`.
static func secureRandomData(count: Int) throws -> Data {

    // Generation method is platform dependent
    // (The Security framework is only available on Apple platforms).
    #if os(Linux)

    var bytes = [UInt8]()
    for _ in 0..<count {
        let randomByte = UInt8.random(in: UInt8.min...UInt8.max)
        bytes.append(randomByte)
    }
    let randomData = Data(bytes: &bytes, count: count)

    return randomData

    #else

    var randomData = Data(count: count)
    let result = try randomData.withUnsafeMutableBytes { bufferPointer -> Int32 in
        guard let baseAddress = bufferPointer.baseAddress else {
            throw Error.zeroRandomBytesRequested
        }

        return SecRandomCopyBytes(kSecRandomDefault, count, baseAddress)
    }

    guard result == errSecSuccess else {
        throw Error.randomBytesGenerationError(statusCode: result)
    }

    return randomData

    #endif
}

Also, another note whilst this is under discussion, specifying types as public has no effect if they are inside internal, private or fileprivate types. So for example: https://github.com/bitmark-inc/tweetnacl-swiftwrap/blob/master/Sources/TweetNacl/TweetNacl.swift#L14 is not exposed publicly because NaclUtil is internal (the default access level if no keyword is provided).

@anhnguyenbitmark Would you like me to open a PR with the above changes, or is this something that is in progress?

ppamorim commented 3 years ago

@anhnguyenbitmark The need came from the Dart library that has these functions exposed.

alfrch commented 3 years ago

Hi @anhnguyenbitmark i tried installing this library using version 1.0.2 through cocoapod but it seems that NaclUtil still not public

Screen Shot 2021-10-28 at 18 28 55

Screen Shot 2021-10-28 at 22 46 12

Screen Shot 2021-10-28 at 22 46 22