Skip to content

Commit

Permalink
fix std.os.getRandomBytes for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed Oct 12, 2017
1 parent b61a6ec commit 7f9dc4e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
7 changes: 6 additions & 1 deletion std/os/index.zig
Expand Up @@ -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);
Expand All @@ -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.
Expand Down
15 changes: 8 additions & 7 deletions 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;
Expand Down

0 comments on commit 7f9dc4e

Please sign in to comment.