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 | |
| 17 | #define LOG_TAG "bcc" |
| 18 | #include <cutils/log.h> |
| 19 | |
| 20 | #include "Sha1Helper.h" |
| 21 | |
| 22 | #include "FileHandle.h" |
| 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 | |
Logan | e132399 | 2011-01-12 04:47:13 +0800 | [diff] [blame] | 32 | #if defined(USE_LIBBCC_SHA1SUM) |
Logan | 75cc8a5 | 2011-01-07 06:06:52 +0800 | [diff] [blame] | 33 | unsigned char sha1LibBCC[20]; |
Logan | 75cc8a5 | 2011-01-07 06:06:52 +0800 | [diff] [blame] | 34 | char const *pathLibBCC = "/system/lib/libbcc.so"; |
Logan | e132399 | 2011-01-12 04:47:13 +0800 | [diff] [blame] | 35 | #endif |
| 36 | |
| 37 | unsigned char sha1LibRS[20]; |
Logan | 75cc8a5 | 2011-01-07 06:06:52 +0800 | [diff] [blame] | 38 | char const *pathLibRS = "/system/lib/libRS.so"; |
| 39 | |
| 40 | |
| 41 | void calcSHA1(unsigned char *result, char const *data, size_t size) { |
| 42 | SHA1_CTX hashContext; |
| 43 | |
| 44 | SHA1Init(&hashContext); |
| 45 | SHA1Update(&hashContext, |
| 46 | reinterpret_cast<const unsigned char *>(data), |
| 47 | static_cast<unsigned long>(size)); |
| 48 | |
| 49 | SHA1Final(result, &hashContext); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | void calcFileSHA1(unsigned char *result, char const *filename) { |
Logan | e132399 | 2011-01-12 04:47:13 +0800 | [diff] [blame] | 54 | #if defined(__arm__) |
| 55 | android::StopWatch calcFileSHA1Timer("calcFileSHA1 time"); |
| 56 | #endif |
| 57 | |
Logan | 75cc8a5 | 2011-01-07 06:06:52 +0800 | [diff] [blame] | 58 | FileHandle file; |
| 59 | |
| 60 | if (file.open(filename, OpenMode::Read) < 0) { |
| 61 | LOGE("Unable to calculate the sha1 checksum of %s\n", filename); |
| 62 | memset(result, '\0', 20); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | SHA1_CTX hashContext; |
| 67 | SHA1Init(&hashContext); |
| 68 | |
| 69 | char buf[256]; |
| 70 | while (true) { |
| 71 | ssize_t nread = file.read(buf, sizeof(buf)); |
Shih-wei Liao | f7cfc02 | 2011-01-07 06:39:53 -0800 | [diff] [blame] | 72 | |
Logan | 75cc8a5 | 2011-01-07 06:06:52 +0800 | [diff] [blame] | 73 | if (nread < 0) { |
| 74 | break; |
| 75 | } |
| 76 | |
| 77 | SHA1Update(&hashContext, |
| 78 | reinterpret_cast<unsigned char *>(buf), |
| 79 | static_cast<unsigned long>(nread)); |
| 80 | |
| 81 | if ((size_t)nread < sizeof(buf)) { |
| 82 | // finished. |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | SHA1Final(result, &hashContext); |
| 88 | } |
| 89 | |
| 90 | } // namespace bcc |