1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
10 // 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"
13 #include "base/logging.h"
17 std::string
SHA1HashString(const std::string
& str
) {
18 ScopedHCRYPTPROV provider
;
19 if (!CryptAcquireContext(provider
.receive(), NULL
, NULL
, PROV_RSA_FULL
,
20 CRYPT_VERIFYCONTEXT
)) {
21 DPLOG(ERROR
) << "CryptAcquireContext failed";
22 return std::string(kSHA1Length
, '\0');
26 ScopedHCRYPTHASH hash
;
27 if (!CryptCreateHash(provider
, CALG_SHA1
, 0, 0, hash
.receive())) {
28 DPLOG(ERROR
) << "CryptCreateHash failed";
29 return std::string(kSHA1Length
, '\0');
32 if (!CryptHashData(hash
, reinterpret_cast<CONST BYTE
*>(str
.data()),
33 static_cast<DWORD
>(str
.length()), 0)) {
34 DPLOG(ERROR
) << "CryptHashData failed";
35 return std::string(kSHA1Length
, '\0');
39 DWORD buffer_size
= sizeof hash_len
;
40 if (!CryptGetHashParam(hash
, HP_HASHSIZE
,
41 reinterpret_cast<unsigned char*>(&hash_len
),
43 DPLOG(ERROR
) << "CryptGetHashParam(HP_HASHSIZE) failed";
44 return std::string(kSHA1Length
, '\0');
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
,
53 DPLOG(ERROR
) << "CryptGetHashParam(HP_HASHVAL) failed";
54 return std::string(kSHA1Length
, '\0');
57 if (hash_len
!= kSHA1Length
) {
58 DLOG(ERROR
) << "Returned hash value is wrong length: " << hash_len
59 << " should be " << kSHA1Length
;
60 return std::string(kSHA1Length
, '\0');