-
Notifications
You must be signed in to change notification settings - Fork 892
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
source_set("browser") { | ||
# Remove when https://github.com/brave/brave-browser/issues/10640 is resolved | ||
check_includes = false | ||
|
||
sources = [ | ||
"crypto_exchange_oauth_util.cc", | ||
"crypto_exchange_oauth_util.h", | ||
] | ||
|
||
deps = [ | ||
"//base", | ||
"//crypto", | ||
] | ||
} |
57 changes: 57 additions & 0 deletions
57
components/crypto_exchange/browser/crypto_exchange_oauth_util.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* Copyright (c) 2020 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
#include <string> | ||
|
||
#include "brave/components/crypto_exchange/browser/crypto_exchange_oauth_util.h" | ||
|
||
#include "base/base64.h" | ||
#include "base/strings/string_number_conversions.h" | ||
#include "crypto/random.h" | ||
#include "crypto/sha2.h" | ||
|
||
namespace crypto_exchange { | ||
|
||
std::string GetCryptoRandomString(bool hex_encode) { | ||
const size_t kSeedByteLength = 32; | ||
uint8_t random_seed_bytes[kSeedByteLength]; | ||
crypto::RandBytes(random_seed_bytes, kSeedByteLength); | ||
|
||
if (!hex_encode) { | ||
std::string encoded_string; | ||
base::Base64Encode( | ||
reinterpret_cast<char*>(random_seed_bytes), &encoded_string); | ||
return encoded_string; | ||
} | ||
|
||
return base::HexEncode( | ||
reinterpret_cast<char*>(random_seed_bytes), kSeedByteLength); | ||
} | ||
|
||
std::string GetCodeChallenge( | ||
const std::string& code_verifier, bool strip_chars) { | ||
std::string code_challenge; | ||
char raw[crypto::kSHA256Length] = {0}; | ||
crypto::SHA256HashString(code_verifier, | ||
raw, | ||
crypto::kSHA256Length); | ||
base::Base64Encode(base::StringPiece(raw, | ||
crypto::kSHA256Length), | ||
&code_challenge); | ||
|
||
if (strip_chars) { | ||
std::replace(code_challenge.begin(), code_challenge.end(), '+', '-'); | ||
std::replace(code_challenge.begin(), code_challenge.end(), '/', '_'); | ||
|
||
code_challenge.erase(std::find_if(code_challenge.rbegin(), | ||
code_challenge.rend(), [](int ch) { | ||
return ch != '='; | ||
}).base(), code_challenge.end()); | ||
} | ||
|
||
return code_challenge; | ||
} | ||
|
||
} // namespace crypto_exchange |
20 changes: 20 additions & 0 deletions
20
components/crypto_exchange/browser/crypto_exchange_oauth_util.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* Copyright (c) 2020 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
#ifndef BRAVE_COMPONENTS_CRYPTO_EXCHANGE_BROWSER_CRYPTO_EXCHANGE_OAUTH_UTIL_H_ | ||
#define BRAVE_COMPONENTS_CRYPTO_EXCHANGE_BROWSER_CRYPTO_EXCHANGE_OAUTH_UTIL_H_ | ||
|
||
#include <string> | ||
|
||
namespace crypto_exchange { | ||
|
||
std::string GetCryptoRandomString(bool hex_encode); | ||
|
||
std::string GetCodeChallenge( | ||
const std::string& code_verifier, bool strip_chars); | ||
|
||
} | ||
|
||
#endif // BRAVE_COMPONENTS_CRYPTO_EXCHANGE_BROWSER_CRYPTO_EXCHANGE_OAUTH_UTIL_H_ |