55import apkr .external .module .ssdeep .exception .SSDeepException ;
66import apkr .external .modules .helpers .log4j .Log ;
77import apkr .external .modules .helpers .log4j .LoggerType ;
8+ import droidefense .util .SSDeep ;
89
910import java .io .*;
1011import java .security .MessageDigest ;
@@ -29,17 +30,25 @@ public static CheckSumGen getInstance() {
2930 public String calculateSSDeep (File f ) throws SSDeepException {
3031 if (f == null ) {
3132 Log .write (LoggerType .ERROR , "Could not create calculateSSDeep() Hash because of a null file reference" );
32- throw new SSDeepException ("Apkr could not calculateSSDeep() SSDeep fuzzing hash for file\n \t " + "\n Possible reason: null file reference." );
33+ throw new SSDeepException ("Droidefense could not calculateSSDeep() SSDeep fuzzing hash for file\n \t " + "\n Possible reason: null file reference." );
3334 }
3435 SsdeepHashGen test = new SsdeepHashGen ();
3536 try {
3637 return test .fuzzy_hash_file (f );
3738 } catch (IOException e ) {
38- Log .write (LoggerType .ERROR , "Apkr could not calculateSSDee()p SSDeep fuzzing hash for file\n \t " + f .getAbsolutePath () + "\n Possible reason: " + e .getLocalizedMessage ());
39- throw new SSDeepException ("Apkr could not calculateSSDee()p SSDeep fuzzing hash for file\n \t " + f .getAbsolutePath () + "\n Possible reason: " + e .getLocalizedMessage ());
39+ Log .write (LoggerType .ERROR , "Droidefense could not calculateSSDee()p SSDeep fuzzing hash for file\n \t " + f .getAbsolutePath () + "\n Possible reason: " + e .getLocalizedMessage ());
40+ throw new SSDeepException ("Droidefense could not calculateSSDee()p SSDeep fuzzing hash for file\n \t " + f .getAbsolutePath () + "\n Possible reason: " + e .getLocalizedMessage ());
4041 }
4142 }
4243
44+ public String calculateSSDeep (byte [] data ) throws SSDeepException {
45+ if (data == null ) {
46+ Log .write (LoggerType .ERROR , "Could not create calculateSSDeep() Hash because of a null file reference" );
47+ throw new SSDeepException ("Droidefense could not calculateSSDeep() SSDeep fuzzing hash for file\n \t " + "\n Possible reason: null file reference." );
48+ }
49+ return SSDeep .generateSSDeep (data );
50+ }
51+
4352 private String calculate (File f , String alg ) throws NoSuchAlgorithmException , IOException {
4453 MessageDigest md = MessageDigest .getInstance (alg );
4554 FileInputStream fis = new FileInputStream (f );
@@ -83,7 +92,7 @@ private String calculate(byte[] data, String alg) throws NoSuchAlgorithmExceptio
8392 public long calculateCRC32 (File f ) throws NullPointerException {
8493 if (f == null ) {
8594 Log .write (LoggerType .ERROR , "Could not create calculateCRC32() Hash because of a null file reference" );
86- throw new NullPointerException ("Apkr could not calculateCRC32() hash for file\n \t " + "\n Possible reason: null file reference." );
95+ throw new NullPointerException ("Droidefense could not calculateCRC32() hash for file\n \t " + "\n Possible reason: null file reference." );
8796 }
8897 try {
8998 InputStream inputStreamn = new FileInputStream (f );
@@ -99,10 +108,20 @@ public long calculateCRC32(File f) throws NullPointerException {
99108 return DEFAULT_RET_CRC32 ;
100109 }
101110
111+ public long calculateCRC32 (byte [] data ) throws NullPointerException {
112+ if (data == null ) {
113+ Log .write (LoggerType .ERROR , "Could not create calculateCRC32() Hash because of a null file reference" );
114+ throw new NullPointerException ("Droidefense could not calculateCRC32() hash for file\n \t " + "\n Possible reason: null file reference." );
115+ }
116+ CRC32 crc = new CRC32 ();
117+ crc .update (data );
118+ return crc .getValue ();
119+ }
120+
102121 public String calculateSHA1 (File f ) throws NullPointerException {
103122 if (f == null ) {
104123 Log .write (LoggerType .ERROR , "Could not create calculateSHA1() Hash because of a null file reference" );
105- throw new NullPointerException ("Apkr could not calculateSHA1() hash for file\n \t " + "\n Possible reason: null file reference." );
124+ throw new NullPointerException ("Droidefense could not calculateSHA1() hash for file\n \t " + "\n Possible reason: null file reference." );
106125 }
107126 try {
108127 return calculate (f , SHA_1 );
@@ -112,10 +131,23 @@ public String calculateSHA1(File f) throws NullPointerException {
112131 return DEFAULT_RET ;
113132 }
114133
134+ public String calculateSHA1 (byte [] data ) throws NullPointerException {
135+ if (data == null ) {
136+ Log .write (LoggerType .ERROR , "Could not create calculateSHA1() Hash because of a null file reference" );
137+ throw new NullPointerException ("Droidefense could not calculateSHA1() hash for file\n \t " + "\n Possible reason: null file reference." );
138+ }
139+ try {
140+ return calculate (data , SHA_1 );
141+ } catch (NoSuchAlgorithmException | IOException e ) {
142+ Log .write (LoggerType .ERROR , "Could not create SHA1 Hash because" , e .getLocalizedMessage (), Arrays .toString (e .getStackTrace ()));
143+ }
144+ return DEFAULT_RET ;
145+ }
146+
115147 public String calculateMD5 (File f ) throws NullPointerException {
116148 if (f == null ) {
117149 Log .write (LoggerType .ERROR , "Could not create calculateSHAMD5() Hash because of a null file reference" );
118- throw new NullPointerException ("Apkr could not calculateMD5() hash for file\n \t " + "\n Possible reason: null file reference." );
150+ throw new NullPointerException ("Droidefense could not calculateMD5() hash for file\n \t " + "\n Possible reason: null file reference." );
119151 }
120152 try {
121153 return calculate (f , MD5 );
@@ -125,10 +157,23 @@ public String calculateMD5(File f) throws NullPointerException {
125157 return DEFAULT_RET ;
126158 }
127159
160+ public String calculateMD5 (byte [] data ) throws NullPointerException {
161+ if (data == null ) {
162+ Log .write (LoggerType .ERROR , "Could not create calculateMD5() Hash because of a null file reference" );
163+ throw new NullPointerException ("Droidefense could not calculateMD5() hash for file\n \t " + "\n Possible reason: null file reference." );
164+ }
165+ try {
166+ return calculate (data , MD5 );
167+ } catch (NoSuchAlgorithmException | IOException e ) {
168+ Log .write (LoggerType .ERROR , "Could not create MD5 Hash because" , e .getLocalizedMessage (), Arrays .toString (e .getStackTrace ()));
169+ }
170+ return DEFAULT_RET ;
171+ }
172+
128173 public String calculateSHA256 (File f ) throws NullPointerException {
129174 if (f == null ) {
130175 Log .write (LoggerType .ERROR , "Could not create calculateSHA256() Hash because of a null file reference" );
131- throw new NullPointerException ("Apkr could not calculateSHA256() hash for file\n \t " + "\n Possible reason: null file reference." );
176+ throw new NullPointerException ("Droidefense could not calculateSHA256() hash for file\n \t " + "\n Possible reason: null file reference." );
132177 }
133178 try {
134179 return calculate (f , SHA_256 );
@@ -141,7 +186,7 @@ public String calculateSHA256(File f) throws NullPointerException {
141186 public String calculateSHA256 (byte [] data ) throws NullPointerException {
142187 if (data == null ) {
143188 Log .write (LoggerType .ERROR , "Could not create calculateSHA256() Hash because of a null file reference" );
144- throw new NullPointerException ("Apkr could not calculateSHA256() hash for file\n \t " + "\n Possible reason: null file reference." );
189+ throw new NullPointerException ("Droidefense could not calculateSHA256() hash for file\n \t " + "\n Possible reason: null file reference." );
145190 }
146191 try {
147192 return calculate (data , SHA_256 );
@@ -154,7 +199,7 @@ public String calculateSHA256(byte[] data) throws NullPointerException {
154199 public String calculateSHA512 (File f ) throws NullPointerException {
155200 if (f == null ) {
156201 Log .write (LoggerType .ERROR , "Could not create calculateSHA512() Hash because of a null file reference" );
157- throw new NullPointerException ("Apkr could not calculateSHA512() hash for file\n \t " + "\n Possible reason: null file reference." );
202+ throw new NullPointerException ("Droidefense could not calculateSHA512() hash for file\n \t " + "\n Possible reason: null file reference." );
158203 }
159204 try {
160205 return calculate (f , SHA_512 );
@@ -163,4 +208,17 @@ public String calculateSHA512(File f) throws NullPointerException {
163208 }
164209 return DEFAULT_RET ;
165210 }
211+
212+ public String calculateSHA512 (byte [] data ) throws NullPointerException {
213+ if (data == null ) {
214+ Log .write (LoggerType .ERROR , "Could not create calculateSHA512() Hash because of a null file reference" );
215+ throw new NullPointerException ("Droidefense could not calculateSHA512() hash for file\n \t " + "\n Possible reason: null file reference." );
216+ }
217+ try {
218+ return calculate (data , SHA_512 );
219+ } catch (NoSuchAlgorithmException | IOException e ) {
220+ Log .write (LoggerType .ERROR , "Could not create SHA512 Hash because" , e .getLocalizedMessage (), Arrays .toString (e .getStackTrace ()));
221+ }
222+ return DEFAULT_RET ;
223+ }
166224}
0 commit comments