29 lines
902 B
Plaintext
29 lines
902 B
Plaintext
// OS randomness for salts and session tokens. (Hashing lives in src/sha256.jai.)
|
|
|
|
//
|
|
// Randomness from the OS.
|
|
//
|
|
|
|
#if OS == .WINDOWS {
|
|
tf_bcrypt :: #system_library "bcrypt";
|
|
BCryptGenRandom :: (algorithm: *void, buffer: *u8, count: u32, flags: u32) -> s32 #foreign tf_bcrypt;
|
|
|
|
random_bytes :: (buffer: *u8, count: int) -> bool {
|
|
BCRYPT_USE_SYSTEM_PREFERRED_RNG :: 2;
|
|
return BCryptGenRandom(null, buffer, cast(u32) count, BCRYPT_USE_SYSTEM_PREFERRED_RNG) == 0;
|
|
}
|
|
} else {
|
|
tf_libc :: #system_library "libc";
|
|
getrandom :: (buffer: *void, count: u64, flags: u32) -> s64 #foreign tf_libc;
|
|
|
|
random_bytes :: (buffer: *u8, count: int) -> bool {
|
|
done := 0;
|
|
while done < count {
|
|
n := getrandom(buffer + done, cast(u64) (count - done), 0);
|
|
if n <= 0 return false;
|
|
done += n;
|
|
}
|
|
return true;
|
|
}
|
|
}
|