Commit 7f9dc4ebc1

Andrew Kelley <superjoe30@gmail.com>
2017-10-12 05:14:48
fix std.os.getRandomBytes for windows
1 parent b61a6ec
Changed files (2)
std
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;
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.