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.
4
5#include "crypto/signature_creator.h"
6
7#include "base/logging.h"
8#include "base/memory/scoped_ptr.h"
9
10namespace crypto {
11
12// static
13SignatureCreator* SignatureCreator::Create(RSAPrivateKey* key) {
14  scoped_ptr<SignatureCreator> result(new SignatureCreator);
15  result->key_ = key;
16
17  if (!CryptCreateHash(key->provider(), CALG_SHA1, 0, 0,
18                       result->hash_object_.receive())) {
19    NOTREACHED();
20    return NULL;
21  }
22
23  return result.release();
24}
25
26SignatureCreator::SignatureCreator() : hash_object_(0) {}
27
28SignatureCreator::~SignatureCreator() {}
29
30bool SignatureCreator::Update(const uint8* data_part, int data_part_len) {
31  if (!CryptHashData(hash_object_, data_part, data_part_len, 0)) {
32    NOTREACHED();
33    return false;
34  }
35
36  return true;
37}
38
39bool SignatureCreator::Final(std::vector<uint8>* signature) {
40  DWORD signature_length = 0;
41  if (!CryptSignHash(hash_object_, AT_SIGNATURE, NULL, 0, NULL,
42                     &signature_length)) {
43    return false;
44  }
45
46  std::vector<uint8> temp;
47  temp.resize(signature_length);
48  if (!CryptSignHash(hash_object_, AT_SIGNATURE, NULL, 0, &temp.front(),
49                     &signature_length)) {
50    return false;
51  }
52
53  temp.resize(signature_length);
54  for (size_t i = temp.size(); i > 0; --i)
55    signature->push_back(temp[i - 1]);
56
57  return true;
58}
59
60}  // namespace crypto
61