blob: da1565dad3ba6a1704654a469b6e0420d0c53b4e [file] [log] [blame]
Logan75cc8a52011-01-07 06:06:52 +08001/*
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
Logane1323992011-01-12 04:47:13 +080026#include <utils/StopWatch.h>
27
Logan75cc8a52011-01-07 06:06:52 +080028#include <sha1.h>
29
30namespace bcc {
31
Logane1323992011-01-12 04:47:13 +080032#if defined(USE_LIBBCC_SHA1SUM)
Logan75cc8a52011-01-07 06:06:52 +080033unsigned char sha1LibBCC[20];
Logan75cc8a52011-01-07 06:06:52 +080034char const *pathLibBCC = "/system/lib/libbcc.so";
Logane1323992011-01-12 04:47:13 +080035#endif
36
37unsigned char sha1LibRS[20];
Logan75cc8a52011-01-07 06:06:52 +080038char const *pathLibRS = "/system/lib/libRS.so";
39
40
41void 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
53void calcFileSHA1(unsigned char *result, char const *filename) {
Logane1323992011-01-12 04:47:13 +080054#if defined(__arm__)
55 android::StopWatch calcFileSHA1Timer("calcFileSHA1 time");
56#endif
57
Logan75cc8a52011-01-07 06:06:52 +080058 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 Liaof7cfc022011-01-07 06:39:53 -080072
Logan75cc8a52011-01-07 06:06:52 +080073 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