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 | |
| 26 | #include <sha1.h> |
| 27 | |
| 28 | namespace bcc { |
| 29 | |
| 30 | |
| 31 | unsigned char sha1LibBCC[20]; |
| 32 | unsigned char sha1LibRS[20]; |
| 33 | |
| 34 | |
| 35 | char const *pathLibBCC = "/system/lib/libbcc.so"; |
| 36 | char const *pathLibRS = "/system/lib/libRS.so"; |
| 37 | |
| 38 | |
| 39 | void calcSHA1(unsigned char *result, char const *data, size_t size) { |
| 40 | SHA1_CTX hashContext; |
| 41 | |
| 42 | SHA1Init(&hashContext); |
| 43 | SHA1Update(&hashContext, |
| 44 | reinterpret_cast<const unsigned char *>(data), |
| 45 | static_cast<unsigned long>(size)); |
| 46 | |
| 47 | SHA1Final(result, &hashContext); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | void calcFileSHA1(unsigned char *result, char const *filename) { |
| 52 | FileHandle file; |
| 53 | |
| 54 | if (file.open(filename, OpenMode::Read) < 0) { |
| 55 | LOGE("Unable to calculate the sha1 checksum of %s\n", filename); |
| 56 | memset(result, '\0', 20); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | SHA1_CTX hashContext; |
| 61 | SHA1Init(&hashContext); |
| 62 | |
| 63 | char buf[256]; |
| 64 | while (true) { |
| 65 | ssize_t nread = file.read(buf, sizeof(buf)); |
| 66 | |
| 67 | if (nread < 0) { |
| 68 | break; |
| 69 | } |
| 70 | |
| 71 | SHA1Update(&hashContext, |
| 72 | reinterpret_cast<unsigned char *>(buf), |
| 73 | static_cast<unsigned long>(nread)); |
| 74 | |
| 75 | if ((size_t)nread < sizeof(buf)) { |
| 76 | // finished. |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | SHA1Final(result, &hashContext); |
| 82 | } |
| 83 | |
| 84 | } // namespace bcc |