blob: b64c9eb858322fcdc828d656326c0bdf63018e71 [file] [log] [blame]
[email protected]4b559b4d2011-04-14 17:37:141// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]94d557e2010-06-23 21:41:402// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/sha1.h"
6
7#include <windows.h>
8#include <wincrypt.h>
9
[email protected]4b559b4d2011-04-14 17:37:1410// This file is not being compiled at the moment (see bug 47218). If we keep
11// sha1 inside base, we cannot depend on src/crypto.
12// #include "crypto/scoped_capi_types.h"
[email protected]94d557e2010-06-23 21:41:4013#include "base/logging.h"
[email protected]94d557e2010-06-23 21:41:4014
15namespace base {
16
[email protected]94d557e2010-06-23 21:41:4017std::string SHA1HashString(const std::string& str) {
[email protected]08ce4d42010-10-21 00:31:1918 ScopedHCRYPTPROV provider;
19 if (!CryptAcquireContext(provider.receive(), NULL, NULL, PROV_RSA_FULL,
20 CRYPT_VERIFYCONTEXT)) {
[email protected]ad8cfa92014-05-21 20:06:2321 DPLOG(ERROR) << "CryptAcquireContext failed";
[email protected]b13b9bd2011-09-28 21:15:2722 return std::string(kSHA1Length, '\0');
[email protected]08ce4d42010-10-21 00:31:1923 }
24
25 {
26 ScopedHCRYPTHASH hash;
27 if (!CryptCreateHash(provider, CALG_SHA1, 0, 0, hash.receive())) {
[email protected]ad8cfa92014-05-21 20:06:2328 DPLOG(ERROR) << "CryptCreateHash failed";
[email protected]b13b9bd2011-09-28 21:15:2729 return std::string(kSHA1Length, '\0');
[email protected]08ce4d42010-10-21 00:31:1930 }
31
32 if (!CryptHashData(hash, reinterpret_cast<CONST BYTE*>(str.data()),
33 static_cast<DWORD>(str.length()), 0)) {
[email protected]ad8cfa92014-05-21 20:06:2334 DPLOG(ERROR) << "CryptHashData failed";
[email protected]b13b9bd2011-09-28 21:15:2735 return std::string(kSHA1Length, '\0');
[email protected]08ce4d42010-10-21 00:31:1936 }
37
38 DWORD hash_len = 0;
39 DWORD buffer_size = sizeof hash_len;
40 if (!CryptGetHashParam(hash, HP_HASHSIZE,
41 reinterpret_cast<unsigned char*>(&hash_len),
42 &buffer_size, 0)) {
[email protected]ad8cfa92014-05-21 20:06:2343 DPLOG(ERROR) << "CryptGetHashParam(HP_HASHSIZE) failed";
[email protected]b13b9bd2011-09-28 21:15:2744 return std::string(kSHA1Length, '\0');
[email protected]08ce4d42010-10-21 00:31:1945 }
46
47 std::string result;
48 if (!CryptGetHashParam(hash, HP_HASHVAL,
49 // We need the + 1 here not because the call will write a trailing \0,
50 // but so that result.length() is correctly set to |hash_len|.
51 reinterpret_cast<BYTE*>(WriteInto(&result, hash_len + 1)), &hash_len,
52 0))) {
[email protected]ad8cfa92014-05-21 20:06:2353 DPLOG(ERROR) << "CryptGetHashParam(HP_HASHVAL) failed";
[email protected]b13b9bd2011-09-28 21:15:2754 return std::string(kSHA1Length, '\0');
[email protected]08ce4d42010-10-21 00:31:1955 }
56
[email protected]b13b9bd2011-09-28 21:15:2757 if (hash_len != kSHA1Length) {
[email protected]a42d4632011-10-26 21:48:0058 DLOG(ERROR) << "Returned hash value is wrong length: " << hash_len
59 << " should be " << kSHA1Length;
[email protected]b13b9bd2011-09-28 21:15:2760 return std::string(kSHA1Length, '\0');
[email protected]08ce4d42010-10-21 00:31:1961 }
62
63 return result;
64 }
[email protected]94d557e2010-06-23 21:41:4065}
66
67} // namespace base