1*14675a02SAndroid Build Coastguard Worker /*
2*14675a02SAndroid Build Coastguard Worker * Copyright 2019 Google LLC
3*14675a02SAndroid Build Coastguard Worker *
4*14675a02SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*14675a02SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*14675a02SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*14675a02SAndroid Build Coastguard Worker *
8*14675a02SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*14675a02SAndroid Build Coastguard Worker *
10*14675a02SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*14675a02SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*14675a02SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*14675a02SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*14675a02SAndroid Build Coastguard Worker * limitations under the License.
15*14675a02SAndroid Build Coastguard Worker */
16*14675a02SAndroid Build Coastguard Worker
17*14675a02SAndroid Build Coastguard Worker #include "fcp/base/random_token.h"
18*14675a02SAndroid Build Coastguard Worker
19*14675a02SAndroid Build Coastguard Worker #include <string.h>
20*14675a02SAndroid Build Coastguard Worker
21*14675a02SAndroid Build Coastguard Worker #include <string>
22*14675a02SAndroid Build Coastguard Worker
23*14675a02SAndroid Build Coastguard Worker #include "absl/strings/escaping.h"
24*14675a02SAndroid Build Coastguard Worker #include "fcp/base/monitoring.h"
25*14675a02SAndroid Build Coastguard Worker #include "openssl/rand.h"
26*14675a02SAndroid Build Coastguard Worker
27*14675a02SAndroid Build Coastguard Worker namespace fcp {
28*14675a02SAndroid Build Coastguard Worker
Generate()29*14675a02SAndroid Build Coastguard Worker RandomToken RandomToken::Generate() {
30*14675a02SAndroid Build Coastguard Worker uint64_t words[2];
31*14675a02SAndroid Build Coastguard Worker static_assert(sizeof(words) == kRandomTokenSizeInBytes,
32*14675a02SAndroid Build Coastguard Worker "Should match the token size");
33*14675a02SAndroid Build Coastguard Worker int r = RAND_bytes(reinterpret_cast<unsigned char*>(words),
34*14675a02SAndroid Build Coastguard Worker kRandomTokenSizeInBytes);
35*14675a02SAndroid Build Coastguard Worker FCP_CHECK(r == 1);
36*14675a02SAndroid Build Coastguard Worker return RandomToken(words[0], words[1]);
37*14675a02SAndroid Build Coastguard Worker }
38*14675a02SAndroid Build Coastguard Worker
FromBytes(absl::Span<char const> bytes)39*14675a02SAndroid Build Coastguard Worker RandomToken RandomToken::FromBytes(absl::Span<char const> bytes) {
40*14675a02SAndroid Build Coastguard Worker FCP_CHECK(bytes.size() == kRandomTokenSizeInBytes);
41*14675a02SAndroid Build Coastguard Worker
42*14675a02SAndroid Build Coastguard Worker uint64_t words[2];
43*14675a02SAndroid Build Coastguard Worker static_assert(sizeof(words) == kRandomTokenSizeInBytes,
44*14675a02SAndroid Build Coastguard Worker "Should match the token size");
45*14675a02SAndroid Build Coastguard Worker memcpy(reinterpret_cast<char*>(words), bytes.data(), kRandomTokenSizeInBytes);
46*14675a02SAndroid Build Coastguard Worker return RandomToken(words[0], words[1]);
47*14675a02SAndroid Build Coastguard Worker }
48*14675a02SAndroid Build Coastguard Worker
ToBytes() const49*14675a02SAndroid Build Coastguard Worker std::array<char, kRandomTokenSizeInBytes> RandomToken::ToBytes() const {
50*14675a02SAndroid Build Coastguard Worker std::array<char, kRandomTokenSizeInBytes> bytes;
51*14675a02SAndroid Build Coastguard Worker memcpy(bytes.data(), reinterpret_cast<char const*>(words_),
52*14675a02SAndroid Build Coastguard Worker kRandomTokenSizeInBytes);
53*14675a02SAndroid Build Coastguard Worker return bytes;
54*14675a02SAndroid Build Coastguard Worker }
55*14675a02SAndroid Build Coastguard Worker
ToString() const56*14675a02SAndroid Build Coastguard Worker std::string RandomToken::ToString() const {
57*14675a02SAndroid Build Coastguard Worker return std::string(reinterpret_cast<char const*>(words_),
58*14675a02SAndroid Build Coastguard Worker kRandomTokenSizeInBytes);
59*14675a02SAndroid Build Coastguard Worker }
60*14675a02SAndroid Build Coastguard Worker
ToPrintableString() const61*14675a02SAndroid Build Coastguard Worker std::string RandomToken::ToPrintableString() const {
62*14675a02SAndroid Build Coastguard Worker return absl::BytesToHexString(absl::string_view(
63*14675a02SAndroid Build Coastguard Worker reinterpret_cast<char const*>(words_), kRandomTokenSizeInBytes));
64*14675a02SAndroid Build Coastguard Worker }
65*14675a02SAndroid Build Coastguard Worker
66*14675a02SAndroid Build Coastguard Worker } // namespace fcp
67