[M94 Dev][Tizen] Fix for errors for generating ninja files
[platform/framework/web/chromium-efl.git] / base / token.cc
1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/token.h"
6
7 #include <inttypes.h>
8
9 #include "base/pickle.h"
10 #include "base/rand_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "third_party/abseil-cpp/absl/types/optional.h"
13
14 namespace base {
15
16 // static
17 Token Token::CreateRandom() {
18   Token token;
19
20   // Use base::RandBytes instead of crypto::RandBytes, because crypto calls the
21   // base version directly, and to prevent the dependency from base/ to crypto/.
22   base::RandBytes(&token, sizeof(token));
23   return token;
24 }
25
26 std::string Token::ToString() const {
27   return base::StringPrintf("%016" PRIX64 "%016" PRIX64, words_[0], words_[1]);
28 }
29
30 void WriteTokenToPickle(Pickle* pickle, const Token& token) {
31   pickle->WriteUInt64(token.high());
32   pickle->WriteUInt64(token.low());
33 }
34
35 absl::optional<Token> ReadTokenFromPickle(PickleIterator* pickle_iterator) {
36   uint64_t high;
37   if (!pickle_iterator->ReadUInt64(&high))
38     return absl::nullopt;
39
40   uint64_t low;
41   if (!pickle_iterator->ReadUInt64(&low))
42     return absl::nullopt;
43
44   return Token(high, low);
45 }
46
47 }  // namespace base