I have a swift server, where currently authentication is handled through a simple email+hashed password combination. I want to replace this with an access token ( + expiration ) so I can remove the storage of email/password on the end-users device to slightly improve security, by making it easier to revoke access from specific end points.
Currently the implementation is cross platform, so I can develop locally ( xcode, macOS ) but run it on my ubuntu box. Therefore I need a cross platform way of generating random numbers.
After some searching obviously I came across /dev/urandom
, so my question is is this safe enough to use? Or should I look into using something like arc4random+chacha20 and if so, why and is this implementation any good?
I am planning on using 128bits as a length, as I have read about 64bits simply not being secure enough ( though probably on my scale it would be. )
for reference, here is my current demo-implementation:
func random_data(_ length: Int) -> Data? {
let stream = open("/dev/urandom", O_RDONLY)
var buffer: [UInt8] = [UInt8](repeating: 0, count: length)
let result = read(stream, &buffer, length)
if result < 0 {
return nil
}
return Data(bytes: buffer)
}
I have a secure strcmp function in my codebase to prevent timing attacks, but in any case I'll most likely be splitting it up in 2 int64's for verification. P.S. I still need to figure out if i want to zero out the memory of the randomly generated token after its work is done. This is a tedious task in swift.