Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef RECOVERY_PRINT_SHA1_H |
| 18 | #define RECOVERY_PRINT_SHA1_H |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <string> |
| 22 | |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 23 | #include <openssl/sha.h> |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 24 | |
Tao Bao | e179276 | 2016-04-19 22:31:01 -0700 | [diff] [blame] | 25 | static std::string print_sha1(const uint8_t* sha1, size_t len) { |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 26 | const char* hex = "0123456789abcdef"; |
| 27 | std::string result = ""; |
| 28 | for (size_t i = 0; i < len; ++i) { |
| 29 | result.push_back(hex[(sha1[i]>>4) & 0xf]); |
| 30 | result.push_back(hex[sha1[i] & 0xf]); |
| 31 | } |
| 32 | return result; |
| 33 | } |
| 34 | |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 35 | static std::string print_sha1(const uint8_t sha1[SHA_DIGEST_LENGTH]) { |
| 36 | return print_sha1(sha1, SHA_DIGEST_LENGTH); |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 39 | static std::string short_sha1(const uint8_t sha1[SHA_DIGEST_LENGTH]) { |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 40 | return print_sha1(sha1, 4); |
| 41 | } |
| 42 | |
Tao Bao | e179276 | 2016-04-19 22:31:01 -0700 | [diff] [blame] | 43 | static std::string print_hex(const uint8_t* bytes, size_t len) { |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 44 | return print_sha1(bytes, len); |
Tao Bao | e179276 | 2016-04-19 22:31:01 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Tao Bao | e6aa332 | 2015-08-05 15:20:27 -0700 | [diff] [blame] | 47 | #endif // RECOVERY_PRINT_SHA1_H |