blob: 0acd6b89b1ed116490edd2ee1308816a0d7ab830 [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
Logan75cc8a52011-01-07 06:06:52 +080017#include "Sha1Helper.h"
18
Logan35849002011-01-15 07:30:43 +080019#include "Config.h"
20
Logan4dcd6792011-02-28 05:12:00 +080021#include "DebugHelper.h"
Logan75cc8a52011-01-07 06:06:52 +080022#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
Joseph Wen2ca6e572011-06-24 14:12:23 -070032unsigned char sha1LibBCC_SHA1[20];
Ying Wang26fea102011-07-05 15:12:25 -070033char const *pathLibBCC_SHA1 = "/system/lib/libbcc.so.sha1";
Logan75cc8a52011-01-07 06:06:52 +080034
Joseph Wen76919072011-07-07 23:06:15 -070035unsigned char sha1LibRS[20];
36char const *pathLibRS = "/system/lib/libRS.so";
37
Logan75cc8a52011-01-07 06:06:52 +080038void 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
50void calcFileSHA1(unsigned char *result, char const *filename) {
Logane1323992011-01-12 04:47:13 +080051 android::StopWatch calcFileSHA1Timer("calcFileSHA1 time");
Logane1323992011-01-12 04:47:13 +080052
Logan75cc8a52011-01-07 06:06:52 +080053 FileHandle file;
54
55 if (file.open(filename, OpenMode::Read) < 0) {
Steve Block10c14122012-01-08 10:15:06 +000056 ALOGE("Unable to calculate the sha1 checksum of %s\n", filename);
Logan75cc8a52011-01-07 06:06:52 +080057 memset(result, '\0', 20);
58 return;
59 }
60
61 SHA1_CTX hashContext;
62 SHA1Init(&hashContext);
63
64 char buf[256];
65 while (true) {
66 ssize_t nread = file.read(buf, sizeof(buf));
Shih-wei Liaof7cfc022011-01-07 06:39:53 -080067
Logan75cc8a52011-01-07 06:06:52 +080068 if (nread < 0) {
69 break;
70 }
71
72 SHA1Update(&hashContext,
73 reinterpret_cast<unsigned char *>(buf),
74 static_cast<unsigned long>(nread));
75
76 if ((size_t)nread < sizeof(buf)) {
77 // finished.
78 break;
79 }
80 }
81
82 SHA1Final(result, &hashContext);
83}
84
Ying Wang26fea102011-07-05 15:12:25 -070085void readSHA1(unsigned char *result, int result_size, char const *filename) {
86 FileHandle file;
87 if (file.open(filename, OpenMode::Read) < 0) {
Steve Block10c14122012-01-08 10:15:06 +000088 ALOGE("Unable to read binary sha1 file %s\n", filename);
Ying Wang26fea102011-07-05 15:12:25 -070089 memset(result, '\0', result_size);
90 return;
91 }
92 file.read((char *)result, result_size);
93}
94
Logan75cc8a52011-01-07 06:06:52 +080095} // namespace bcc