| ... | ... | @@ -130,16 +130,10 @@ pub fn getRandomBytes(buf: []u8) !void { |
| 130 | 130 | try posixRead(fd, buf); |
| 131 | 131 | }, |
| 132 | 132 | Os.windows => { |
| 133 | | var hCryptProv: windows.HCRYPTPROV = undefined; |
| 134 | | if (windows.CryptAcquireContextA(&hCryptProv, null, null, windows.PROV_RSA_FULL, 0) == 0) { |
| 135 | | const err = windows.GetLastError(); |
| 136 | | return switch (err) { |
| 137 | | else => unexpectedErrorWindows(err), |
| 138 | | }; |
| 139 | | } |
| 140 | | defer _ = windows.CryptReleaseContext(hCryptProv, 0); |
| 141 | | |
| 142 | | if (windows.CryptGenRandom(hCryptProv, @intCast(windows.DWORD, buf.len), buf.ptr) == 0) { |
| 133 | // Call RtlGenRandom() instead of CryptGetRandom() on Windows |
| 134 | // https://github.com/rust-lang-nursery/rand/issues/111 |
| 135 | // https://bugzilla.mozilla.org/show_bug.cgi?id=504270 |
| 136 | if (windows.RtlGenRandom(buf.ptr, buf.len) == 0) { |
| 143 | 137 | const err = windows.GetLastError(); |
| 144 | 138 | return switch (err) { |
| 145 | 139 | else => unexpectedErrorWindows(err), |
| ... | ... | @@ -159,8 +153,14 @@ pub fn getRandomBytes(buf: []u8) !void { |
| 159 | 153 | } |
| 160 | 154 | |
| 161 | 155 | test "os.getRandomBytes" { |
| 162 | | var buf: [50]u8 = undefined; |
| 163 | | try getRandomBytes(buf[0..]); |
| 156 | var buf_a: [50]u8 = undefined; |
| 157 | var buf_b: [50]u8 = undefined; |
| 158 | // Call Twice |
| 159 | try getRandomBytes(buf_a[0..]); |
| 160 | try getRandomBytes(buf_b[0..]); |
| 161 | |
| 162 | // Check if random (not 100% conclusive) |
| 163 | assert( !mem.eql(u8, buf_a, buf_b) ); |
| 164 | 164 | } |
| 165 | 165 | |
| 166 | 166 | /// Raises a signal in the current kernel thread, ending its execution. |