Use WTF::cryptographicallyRandomValues in FileSystemWin.cpp
authorparoga@webkit.org <paroga@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 21 Feb 2012 15:52:06 +0000 (15:52 +0000)
committerparoga@webkit.org <paroga@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 21 Feb 2012 15:52:06 +0000 (15:52 +0000)
https://bugs.webkit.org/show_bug.cgi?id=79089

Reviewed by Adam Roben.

Avoid loading and unloading of the crypto library during every
call to openTemporaryFile() and make the code easier.

* platform/win/FileSystemWin.cpp:
(WebCore::openTemporaryFile):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@108353 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/platform/win/FileSystemWin.cpp

index f2affa5..4ccad15 100644 (file)
@@ -1,3 +1,16 @@
+2012-02-21  Patrick Gansterer  <paroga@webkit.org>
+
+        Use WTF::cryptographicallyRandomValues in FileSystemWin.cpp
+        https://bugs.webkit.org/show_bug.cgi?id=79089
+
+        Reviewed by Adam Roben.
+
+        Avoid loading and unloading of the crypto library during every
+        call to openTemporaryFile() and make the code easier.
+
+        * platform/win/FileSystemWin.cpp:
+        (WebCore::openTemporaryFile):
+
 2012-02-21  Alexander Færøy  <alexander.faeroy@nokia.com>
 
         Remove stylesheet pointer from StylePropertySet
index e862dac..1187544 100644 (file)
@@ -32,6 +32,7 @@
 
 #include "NotImplemented.h"
 #include "PathWalker.h"
+#include <wtf/CryptographicallyRandomNumber.h>
 #include <wtf/HashMap.h>
 #include <wtf/text/CString.h>
 #include <wtf/text/WTFString.h>
@@ -220,16 +221,11 @@ String openTemporaryFile(const String&, PlatformFileHandle& handle)
     if (tempPathLength <= 0 || tempPathLength > WTF_ARRAY_LENGTH(tempPath))
         return String();
 
-    HCRYPTPROV hCryptProv = 0;
-    if (!CryptAcquireContext(&hCryptProv, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
-        return String();
-
     String proposedPath;
     do {
         wchar_t tempFile[] = L"XXXXXXXX.tmp"; // Use 8.3 style name (more characters aren't helpful due to 8.3 short file names)
         const int randomPartLength = 8;
-        if (!CryptGenRandom(hCryptProv, randomPartLength * sizeof(wchar_t), reinterpret_cast<BYTE*>(tempFile)))
-            break;
+        cryptographicallyRandomValues(tempFile, randomPartLength * sizeof(wchar_t));
 
         // Limit to valid filesystem characters, also excluding others that could be problematic, like punctuation.
         // don't include both upper and lowercase since Windows file systems are typically not case sensitive.
@@ -247,8 +243,6 @@ String openTemporaryFile(const String&, PlatformFileHandle& handle)
         handle = ::CreateFileW(proposedPath.charactersWithNullTermination(), GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);
     } while (!isHandleValid(handle) && GetLastError() == ERROR_ALREADY_EXISTS);
 
-    CryptReleaseContext(hCryptProv, 0);
-
     if (!isHandleValid(handle))
         return String();