Skip to content

Commit d9d59ce

Browse files
Expose the Crypto class (#1102)
Expose the Crypto class with Sha256 and HmacSha256 methods. These methods are useful for users who need to store and compute and use hashes. Adapt to coding style. Relates-To: OAM-792 Signed-off-by: Mykhailo Kuchma <[email protected]>
1 parent cd84049 commit d9d59ce

File tree

6 files changed

+273
-165
lines changed

6 files changed

+273
-165
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (C) 2019-2020 HERE Europe B.V.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
#pragma once
21+
22+
#include <array>
23+
#include <string>
24+
#include <vector>
25+
26+
#include <olp/authentication/AuthenticationApi.h>
27+
28+
namespace olp {
29+
namespace authentication {
30+
31+
/**
32+
* @brief Crypto class exposes the cryptographic algoritms used by the library.
33+
*/
34+
class AUTHENTICATION_API Crypto {
35+
public:
36+
static const size_t Sha256DigestLength = 32;
37+
using Sha256Digest = std::array<unsigned char, Sha256DigestLength>;
38+
39+
/**
40+
* @brief Sha256 cryptographic function used to compute the hash value for a
41+
* buffer.
42+
*
43+
* @param content A vector of unsigned chars for which the hash is computed.
44+
*
45+
* @return An array of 32 bytes that represent a hash value.
46+
*/
47+
static Sha256Digest Sha256(const std::vector<unsigned char>& content);
48+
49+
/**
50+
* @brief HMAC function based on SHA-256 hash function.
51+
*
52+
* @param key A key.
53+
* @param message A message.
54+
*
55+
* @return An array of 32 bytes that represent a hash value.
56+
*/
57+
static Sha256Digest HmacSha256(const std::string& key,
58+
const std::string& message);
59+
};
60+
61+
} // namespace authentication
62+
} // namespace olp

olp-cpp-sdk-authentication/src/AuthenticationClientUtils.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include <rapidjson/writer.h>
3030

3131
#include "Constants.h"
32-
#include "Crypto.h"
32+
#include <olp/authentication/Crypto.h>
3333
#include "olp/core/http/NetworkUtils.h"
3434
#include "olp/core/utils/Base64.h"
3535
#include "olp/core/utils/Url.h"
@@ -55,8 +55,8 @@ constexpr auto kOauthSignatureMethod = "oauth_signature_method";
5555
constexpr auto kVersion = "1.0";
5656
constexpr auto kHmac = "HMAC-SHA256";
5757

58-
std::string Base64Encode(const std::vector<uint8_t>& vector) {
59-
std::string ret = olp::utils::Base64Encode(vector);
58+
std::string Base64Encode(const Crypto::Sha256Digest& digest) {
59+
std::string ret = olp::utils::Base64Encode(digest.data(), digest.size());
6060
// Base64 encode sometimes return multiline with garbage at the end
6161
if (!ret.empty()) {
6262
auto loc = ret.find(kLineFeed);
@@ -299,7 +299,7 @@ std::string GenerateAuthorizationHeader(
299299
<< encoded_query;
300300

301301
const std::string encode_key = credentials.GetSecret() + kParamAdd;
302-
auto hmac_result = Crypto::hmac_sha256(encode_key, signature_base.str());
302+
auto hmac_result = Crypto::HmacSha256(encode_key, signature_base.str());
303303
auto signature = Base64Encode(hmac_result);
304304

305305
std::stringstream authorization;

0 commit comments

Comments
 (0)