Closed ppamorim closed 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.
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?
@anhnguyenbitmark The need came from the Dart library that has these functions exposed.
Hi @anhnguyenbitmark i tried installing this library using version 1.0.2 through cocoapod but it seems that NaclUtil still not public
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.