Skip to content

Commit

Permalink
Creating crypto_exchange module
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanml committed Aug 3, 2020
1 parent 5809216 commit e007a15
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
6 changes: 6 additions & 0 deletions browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@ source_set("browser_process") {
]
}

if (gemini_enabled || binance_enabled) {
deps += [
"//brave/components/crypto_exchange/browser",
]
}

if (brave_together_enabled) {
sources += [
"brave_together/brave_together_util.cc",
Expand Down
14 changes: 14 additions & 0 deletions components/crypto_exchange/browser/BUILD.gn
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 components/crypto_exchange/browser/crypto_exchange_oauth_util.cc
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 components/crypto_exchange/browser/crypto_exchange_oauth_util.h
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_

0 comments on commit e007a15

Please sign in to comment.