Revert 187455 "Fix bug where calling into the chrome object of a..."
[chromium-blink-merge.git] / crypto / signature_creator_unittest.cc
blob2d69223f748a51c3df3b3a8eae5a42799f35ed50
1 // Copyright (c) 2012 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.
5 #include <vector>
7 #include "base/memory/scoped_ptr.h"
8 #include "crypto/rsa_private_key.h"
9 #include "crypto/signature_creator.h"
10 #include "crypto/signature_verifier.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 TEST(SignatureCreatorTest, BasicTest) {
14 // Do a verify round trip.
15 scoped_ptr<crypto::RSAPrivateKey> key_original(
16 crypto::RSAPrivateKey::Create(1024));
17 ASSERT_TRUE(key_original.get());
19 std::vector<uint8> key_info;
20 key_original->ExportPrivateKey(&key_info);
21 scoped_ptr<crypto::RSAPrivateKey> key(
22 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_info));
23 ASSERT_TRUE(key.get());
25 scoped_ptr<crypto::SignatureCreator> signer(
26 crypto::SignatureCreator::Create(key.get()));
27 ASSERT_TRUE(signer.get());
29 std::string data("Hello, World!");
30 ASSERT_TRUE(signer->Update(reinterpret_cast<const uint8*>(data.c_str()),
31 data.size()));
33 std::vector<uint8> signature;
34 ASSERT_TRUE(signer->Final(&signature));
36 std::vector<uint8> public_key_info;
37 ASSERT_TRUE(key_original->ExportPublicKey(&public_key_info));
39 // This is the algorithm ID for SHA-1 with RSA encryption.
40 // TODO(aa): Factor this out into some shared location.
41 const uint8 kSHA1WithRSAAlgorithmID[] = {
42 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
43 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00
45 crypto::SignatureVerifier verifier;
46 ASSERT_TRUE(verifier.VerifyInit(
47 kSHA1WithRSAAlgorithmID, sizeof(kSHA1WithRSAAlgorithmID),
48 &signature.front(), signature.size(),
49 &public_key_info.front(), public_key_info.size()));
51 verifier.VerifyUpdate(reinterpret_cast<const uint8*>(data.c_str()),
52 data.size());
53 ASSERT_TRUE(verifier.VerifyFinal());