Line data Source code
1 : /**
2 : * Copyright Soramitsu Co., Ltd. 2018 All Rights Reserved.
3 : * http://soramitsu.co.jp
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : #include "cryptography/blob.hpp"
19 :
20 : #include "common/byteutils.hpp"
21 :
22 : namespace shared_model {
23 : namespace crypto {
24 :
25 : std::string toBinaryString(const Blob &b) {
26 284762 : return std::string(b.blob().begin(), b.blob().end());
27 : }
28 :
29 : Blob::Blob(const std::string &blob)
30 115647 : : Blob(Bytes(blob.begin(), blob.end())) {}
31 :
32 : Blob::Blob(const Bytes &blob) : Blob(Bytes(blob)) {}
33 :
34 : Blob::Blob(Bytes &&blob) noexcept : blob_(std::move(blob)) {
35 210202 : hex_ = iroha::bytestringToHexstring(toBinaryString(*this));
36 210202 : }
37 :
38 : Blob *Blob::clone() const {
39 1 : return new Blob(blob());
40 0 : }
41 :
42 : bool Blob::operator==(const Blob &rhs) const {
43 35119 : return blob() == rhs.blob();
44 : }
45 :
46 : Blob Blob::fromHexString(const std::string &hex) {
47 : using iroha::operator|;
48 4379 : Blob b("");
49 : iroha::hexstringToBytestring(hex) | [&](auto &&s) { b = Blob(s); };
50 4379 : return b;
51 4379 : }
52 :
53 : const Blob::Bytes &Blob::blob() const {
54 704434 : return blob_;
55 : }
56 :
57 : const std::string &Blob::hex() const {
58 41950 : return hex_;
59 : }
60 :
61 : size_t Blob::size() const {
62 305 : return blob_.size();
63 : }
64 :
65 : std::string Blob::toString() const {
66 0 : return detail::PrettyStringBuilder()
67 0 : .init("Blob")
68 0 : .append(hex())
69 0 : .finalize();
70 0 : }
71 :
72 : } // namespace crypto
73 : } // namespace shared_model
|