OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 | 6 |
7 #include "crypto/hmac.h" | 7 #include "crypto/hmac.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 static const size_t kSHA1DigestSize = 20; | 10 static const size_t kSHA1DigestSize = 20; |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
268 base::StringPiece(kSimpleHmacCases[i].data, | 268 base::StringPiece(kSimpleHmacCases[i].data, |
269 kSimpleHmacCases[i].data_len))); | 269 kSimpleHmacCases[i].data_len))); |
270 | 270 |
271 // Expected size, mismatched data | 271 // Expected size, mismatched data |
272 EXPECT_FALSE(hmac.Verify( | 272 EXPECT_FALSE(hmac.Verify( |
273 base::StringPiece(kSimpleHmacCases[i].data, | 273 base::StringPiece(kSimpleHmacCases[i].data, |
274 kSimpleHmacCases[i].data_len), | 274 kSimpleHmacCases[i].data_len), |
275 base::StringPiece(empty_digest, kSHA1DigestSize))); | 275 base::StringPiece(empty_digest, kSHA1DigestSize))); |
276 } | 276 } |
277 } | 277 } |
278 | |
279 TEST(HMACTest, EmptyKey) { | |
280 // Test vector from https://en.wikipedia.org/wiki/HMAC | |
281 const char* kExpectedDigest = | |
282 "\xFB\xDB\x1D\x1B\x18\xAA\x6C\x08\x32\x4B\x7D\x64\xB7\x1F\xB7\x63" | |
283 "\x70\x69\x0E\x1D"; | |
284 base::StringPiece data("", 0u); | |
wtc
2014/04/10 18:43:41
You can also try using a std::string:
std::stri
davidben
2014/04/10 18:54:29
Yeah, per C++11, std::string::data and std::string
| |
285 | |
286 crypto::HMAC hmac(crypto::HMAC::SHA1); | |
287 ASSERT_TRUE(hmac.Init(NULL, 0)); | |
288 | |
289 unsigned char digest[kSHA1DigestSize]; | |
290 EXPECT_TRUE(hmac.Sign(data, digest, kSHA1DigestSize)); | |
291 EXPECT_EQ(0, memcmp(kExpectedDigest, digest, kSHA1DigestSize)); | |
292 | |
293 EXPECT_TRUE(hmac.Verify( | |
294 data, base::StringPiece(kExpectedDigest, kSHA1DigestSize))); | |
295 } | |
OLD | NEW |