zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 7f9dc4ebc10eb13e73466224f28ba62747693df9 (tree)
parent b61a6ec8a6ba7222efd3749a9c5ae30db7e4ef6b
Author: Andrew Kelley <superjoe30@gmail.com>
Date:   Wed, 11 Oct 2017 23:14:48 -0400

fix std.os.getRandomBytes for windows

Diffstat:
Mstd/os/index.zig | 7++++++-
Mstd/os/windows/index.zig | 15++++++++-------
2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/std/os/index.zig b/std/os/index.zig @@ -85,7 +85,7 @@ pub fn getRandomBytes(buf: []u8) -> %void { }, Os.windows => { var hCryptProv: windows.HCRYPTPROV = undefined; - if (!windows.CryptAcquireContext(&hCryptProv, null, null, windows.PROV_RSA_FULL, 0)) { + if (!windows.CryptAcquireContextA(&hCryptProv, null, null, windows.PROV_RSA_FULL, 0)) { return error.Unexpected; } defer _ = windows.CryptReleaseContext(hCryptProv, 0); @@ -98,6 +98,11 @@ pub fn getRandomBytes(buf: []u8) -> %void { } } +test "os.getRandomBytes" { + var buf: [50]u8 = undefined; + %%getRandomBytes(buf[0..]); +} + /// Raises a signal in the current kernel thread, ending its execution. /// If linking against libc, this calls the abort() libc function. Otherwise /// it uses the zig standard library implementation. diff --git a/std/os/windows/index.zig b/std/os/windows/index.zig @@ -1,18 +1,19 @@ pub const ERROR = @import("error.zig"); +pub extern "advapi32" stdcallcc fn CryptAcquireContextA(phProv: &HCRYPTPROV, pszContainer: ?LPCSTR, + pszProvider: ?LPCSTR, dwProvType: DWORD, dwFlags: DWORD) -> bool; + +pub extern "advapi32" stdcallcc fn CryptReleaseContext(hProv: HCRYPTPROV, dwFlags: DWORD) -> bool; + +pub extern "advapi32" stdcallcc fn CryptGenRandom(hProv: HCRYPTPROV, dwLen: DWORD, pbBuffer: &BYTE) -> bool; + + pub extern "kernel32" stdcallcc fn CloseHandle(hObject: HANDLE) -> BOOL; pub extern "kernel32" stdcallcc fn CreateFileA(lpFileName: LPCSTR, dwDesiredAccess: DWORD, dwShareMode: DWORD, lpSecurityAttributes: ?LPSECURITY_ATTRIBUTES, dwCreationDisposition: DWORD, dwFlagsAndAttributes: DWORD, hTemplateFile: ?HANDLE) -> HANDLE; -pub extern "kernel32" stdcallcc fn CryptAcquireContext(phProv: &HCRYPTPROV, pszContainer: LPCTSTR, - pszProvider: LPCTSTR, dwProvType: DWORD, dwFlags: DWORD) -> bool; - -pub extern "kernel32" stdcallcc fn CryptReleaseContext(hProv: HCRYPTPROV, dwFlags: DWORD) -> bool; - -pub extern "kernel32" stdcallcc fn CryptGenRandom(hProv: HCRYPTPROV, dwLen: DWORD, pbBuffer: &BYTE) -> bool; - pub extern "kernel32" stdcallcc fn DeleteFileA(lpFileName: LPCSTR) -> bool; pub extern "kernel32" stdcallcc fn ExitProcess(exit_code: UINT) -> noreturn;