summaryrefslogtreecommitdiffstats
path: root/base/sha1_win.cc
blob: 98c3840f0e47a9087722ee83f5a339682a9dca80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/sha1.h"

#include <windows.h>
#include <wincrypt.h>

// This file is not being compiled at the moment (see bug 47218). If we keep
// sha1 inside base, we cannot depend on src/crypto.
// #include "crypto/scoped_capi_types.h"
#include "base/logging.h"

namespace base {

std::string SHA1HashString(const std::string& str) {
  ScopedHCRYPTPROV provider;
  if (!CryptAcquireContext(provider.receive(), NULL, NULL, PROV_RSA_FULL,
                           CRYPT_VERIFYCONTEXT)) {
    DLOG_GETLASTERROR(ERROR) << "CryptAcquireContext failed";
    return std::string(kSHA1Length, '\0');
  }

  {
    ScopedHCRYPTHASH hash;
    if (!CryptCreateHash(provider, CALG_SHA1, 0, 0, hash.receive())) {
      DLOG_GETLASTERROR(ERROR) << "CryptCreateHash failed";
      return std::string(kSHA1Length, '\0');
    }

    if (!CryptHashData(hash, reinterpret_cast<CONST BYTE*>(str.data()),
                       static_cast<DWORD>(str.length()), 0)) {
      DLOG_GETLASTERROR(ERROR) << "CryptHashData failed";
      return std::string(kSHA1Length, '\0');
    }

    DWORD hash_len = 0;
    DWORD buffer_size = sizeof hash_len;
    if (!CryptGetHashParam(hash, HP_HASHSIZE,
                           reinterpret_cast<unsigned char*>(&hash_len),
                           &buffer_size, 0)) {
      DLOG_GETLASTERROR(ERROR) << "CryptGetHashParam(HP_HASHSIZE) failed";
      return std::string(kSHA1Length, '\0');
    }

    std::string result;
    if (!CryptGetHashParam(hash, HP_HASHVAL,
        // We need the + 1 here not because the call will write a trailing \0,
        // but so that result.length() is correctly set to |hash_len|.
        reinterpret_cast<BYTE*>(WriteInto(&result, hash_len + 1)), &hash_len,
        0))) {
      DLOG_GETLASTERROR(ERROR) << "CryptGetHashParam(HP_HASHVAL) failed";
      return std::string(kSHA1Length, '\0');
    }

    if (hash_len != kSHA1Length) {
      DLOG(ERROR) << "Returned hash value is wrong length: " << hash_len
                  << " should be " << kSHA1Length;
      return std::string(kSHA1Length, '\0');
    }

    return result;
  }
}

}  // namespace base