blob: 90d5ac6d81deed590af35d47e7f4665a32e2497f [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
Logan35849002011-01-15 07:30:43 +080022#include "Config.h"
23
Logan75cc8a52011-01-07 06:06:52 +080024#include "FileHandle.h"
25
26#include <string.h>
27
Logane1323992011-01-12 04:47:13 +080028#include <utils/StopWatch.h>
29
Logan75cc8a52011-01-07 06:06:52 +080030#include <sha1.h>
31
32namespace bcc {
33
Logan35849002011-01-15 07:30:43 +080034#if USE_LIBBCC_SHA1SUM
Logan75cc8a52011-01-07 06:06:52 +080035unsigned char sha1LibBCC[20];
Logan75cc8a52011-01-07 06:06:52 +080036char const *pathLibBCC = "/system/lib/libbcc.so";
Logane1323992011-01-12 04:47:13 +080037#endif
38
39unsigned char sha1LibRS[20];
Logan75cc8a52011-01-07 06:06:52 +080040char const *pathLibRS = "/system/lib/libRS.so";
41
42
43void calcSHA1(unsigned char *result, char const *data, size_t size) {
44 SHA1_CTX hashContext;
45
46 SHA1Init(&hashContext);
47 SHA1Update(&hashContext,
48 reinterpret_cast<const unsigned char *>(data),
49 static_cast<unsigned long>(size));
50
51 SHA1Final(result, &hashContext);
52}
53
54
55void calcFileSHA1(unsigned char *result, char const *filename) {
Logane1323992011-01-12 04:47:13 +080056#if defined(__arm__)
57 android::StopWatch calcFileSHA1Timer("calcFileSHA1 time");
58#endif
59
Logan75cc8a52011-01-07 06:06:52 +080060 FileHandle file;
61
62 if (file.open(filename, OpenMode::Read) < 0) {
63 LOGE("Unable to calculate the sha1 checksum of %s\n", filename);
64 memset(result, '\0', 20);
65 return;
66 }
67
68 SHA1_CTX hashContext;
69 SHA1Init(&hashContext);
70
71 char buf[256];
72 while (true) {
73 ssize_t nread = file.read(buf, sizeof(buf));
Shih-wei Liaof7cfc022011-01-07 06:39:53 -080074
Logan75cc8a52011-01-07 06:06:52 +080075 if (nread < 0) {
76 break;
77 }
78
79 SHA1Update(&hashContext,
80 reinterpret_cast<unsigned char *>(buf),
81 static_cast<unsigned long>(nread));
82
83 if ((size_t)nread < sizeof(buf)) {
84 // finished.
85 break;
86 }
87 }
88
89 SHA1Final(result, &hashContext);
90}
91
92} // namespace bcc