Logan | 75cc8a5 | 2011-01-07 06:06:52 +0800 | [diff] [blame] | 1 | /* |
| 2 | * copyright 2010, 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 | |
Logan | 75cc8a5 | 2011-01-07 06:06:52 +0800 | [diff] [blame] | 17 | #include "Sha1Helper.h" |
| 18 | |
Logan | 3584900 | 2011-01-15 07:30:43 +0800 | [diff] [blame] | 19 | #include "Config.h" |
| 20 | |
Logan | 4dcd679 | 2011-02-28 05:12:00 +0800 | [diff] [blame] | 21 | #include "DebugHelper.h" |
Zonr Chang | d670be7 | 2012-04-05 15:09:28 +0800 | [diff] [blame^] | 22 | #include "InputFile.h" |
Logan | 75cc8a5 | 2011-01-07 06:06:52 +0800 | [diff] [blame] | 23 | |
| 24 | #include <string.h> |
| 25 | |
Logan | e132399 | 2011-01-12 04:47:13 +0800 | [diff] [blame] | 26 | #include <utils/StopWatch.h> |
| 27 | |
Logan | 75cc8a5 | 2011-01-07 06:06:52 +0800 | [diff] [blame] | 28 | #include <sha1.h> |
| 29 | |
| 30 | namespace bcc { |
| 31 | |
Joseph Wen | 2ca6e57 | 2011-06-24 14:12:23 -0700 | [diff] [blame] | 32 | unsigned char sha1LibBCC_SHA1[20]; |
Ying Wang | 26fea10 | 2011-07-05 15:12:25 -0700 | [diff] [blame] | 33 | char const *pathLibBCC_SHA1 = "/system/lib/libbcc.so.sha1"; |
Logan | 75cc8a5 | 2011-01-07 06:06:52 +0800 | [diff] [blame] | 34 | |
Joseph Wen | 7691907 | 2011-07-07 23:06:15 -0700 | [diff] [blame] | 35 | unsigned char sha1LibRS[20]; |
| 36 | char const *pathLibRS = "/system/lib/libRS.so"; |
| 37 | |
Logan | 75cc8a5 | 2011-01-07 06:06:52 +0800 | [diff] [blame] | 38 | void calcSHA1(unsigned char *result, char const *data, size_t size) { |
| 39 | SHA1_CTX hashContext; |
| 40 | |
| 41 | SHA1Init(&hashContext); |
| 42 | SHA1Update(&hashContext, |
| 43 | reinterpret_cast<const unsigned char *>(data), |
| 44 | static_cast<unsigned long>(size)); |
| 45 | |
| 46 | SHA1Final(result, &hashContext); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | void calcFileSHA1(unsigned char *result, char const *filename) { |
Logan | e132399 | 2011-01-12 04:47:13 +0800 | [diff] [blame] | 51 | android::StopWatch calcFileSHA1Timer("calcFileSHA1 time"); |
Logan | e132399 | 2011-01-12 04:47:13 +0800 | [diff] [blame] | 52 | |
Zonr Chang | d670be7 | 2012-04-05 15:09:28 +0800 | [diff] [blame^] | 53 | InputFile file(filename); |
Logan | 75cc8a5 | 2011-01-07 06:06:52 +0800 | [diff] [blame] | 54 | |
Zonr Chang | d670be7 | 2012-04-05 15:09:28 +0800 | [diff] [blame^] | 55 | if (file.hasError()) { |
| 56 | ALOGE("Unable to open the file %s before SHA-1 checksum " |
| 57 | "calculation! (%s)", filename, file.getErrorMessage().c_str()); |
Logan | 75cc8a5 | 2011-01-07 06:06:52 +0800 | [diff] [blame] | 58 | memset(result, '\0', 20); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | SHA1_CTX hashContext; |
| 63 | SHA1Init(&hashContext); |
| 64 | |
| 65 | char buf[256]; |
| 66 | while (true) { |
| 67 | ssize_t nread = file.read(buf, sizeof(buf)); |
Shih-wei Liao | f7cfc02 | 2011-01-07 06:39:53 -0800 | [diff] [blame] | 68 | |
Logan | 75cc8a5 | 2011-01-07 06:06:52 +0800 | [diff] [blame] | 69 | if (nread < 0) { |
| 70 | break; |
| 71 | } |
| 72 | |
| 73 | SHA1Update(&hashContext, |
| 74 | reinterpret_cast<unsigned char *>(buf), |
| 75 | static_cast<unsigned long>(nread)); |
| 76 | |
| 77 | if ((size_t)nread < sizeof(buf)) { |
| 78 | // finished. |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | SHA1Final(result, &hashContext); |
| 84 | } |
| 85 | |
Ying Wang | 26fea10 | 2011-07-05 15:12:25 -0700 | [diff] [blame] | 86 | void readSHA1(unsigned char *result, int result_size, char const *filename) { |
Zonr Chang | d670be7 | 2012-04-05 15:09:28 +0800 | [diff] [blame^] | 87 | InputFile file(filename); |
| 88 | if (file.hasError()) { |
| 89 | ALOGE("Unable to open the binary sha1 file %s! (%s)", filename, |
| 90 | file.getErrorMessage().c_str()); |
Ying Wang | 26fea10 | 2011-07-05 15:12:25 -0700 | [diff] [blame] | 91 | memset(result, '\0', result_size); |
| 92 | return; |
| 93 | } |
| 94 | file.read((char *)result, result_size); |
| 95 | } |
| 96 | |
Logan | 75cc8a5 | 2011-01-07 06:06:52 +0800 | [diff] [blame] | 97 | } // namespace bcc |