Commit 16cfb3547e3bc866b12b6e85f16ef3de4495b1a0

Authored by Maryna German Bjørnstrøm
1 parent 5bce4d85

Del 3. Verifisering SHA256 hash verdien for en fil mot lagret verdi i

hashfil.txt
Showing 2 changed files with 68 additions and 7 deletions   Show diff stats
hashfil.txt
1   -C:\Users\paul\git\Oblig5\files\firstFolder\names.txt,59f627f0b0b42327917128abb4a71486f57642807fcba6b58414a12fe9670b43C:\Users\paul\git\Oblig5\files\secondFolder\phones.txt,9ce6a51fe5769a96c7caa33169c77c83b9785ae4f75b05d3ce8d68bd08c8a723C:\Users\paul\git\Oblig5\files\firstFolder\names.txt,59f627f0b0b42327917128abb4a71486f57642807fcba6b58414a12fe9670b43
2   -C:\Users\paul\git\Oblig5\files\secondFolder\phones.txt,9ce6a51fe5769a96c7caa33169c77c83b9785ae4f75b05d3ce8d68bd08c8a723
3 1 \ No newline at end of file
  2 +files/secondFolder/phones.txt,9ce6a51fe5769a96c7caa33169c77c83b9785ae4f75b05d3ce8d68bd08c8a723
  3 +files/firstFolder/names.txt,59f627f0b0b42327917128abb4a71486f57642807fcba6b58414a12fe9670b43
4 4 \ No newline at end of file
... ...
src/hashObl5/ExecutableFiles.java
... ... @@ -3,6 +3,8 @@ package hashObl5;
3 3 import java.io.*;
4 4 import java.security.*;
5 5 import java.security.spec.X509EncodedKeySpec;
  6 +import java.util.HashMap;
  7 +import java.util.Map;
6 8  
7 9 import org.apache.commons.codec.digest.DigestUtils;
8 10  
... ... @@ -76,7 +78,7 @@ public class ExecutableFiles extends Object {
76 78 }
77 79  
78 80 // skriv filnavn og sha256hash til fil
79   - skrivHash(toInfect.getAbsolutePath(), apache_sha256);
  81 + skrivHash(toInfect.getPath(), apache_sha256);
80 82 }
81 83  
82 84  
... ... @@ -222,21 +224,80 @@ public class ExecutableFiles extends Object {
222 224 return false;
223 225 }
224 226 }
  227 +
  228 +
  229 + private static boolean verifisereHash(String filePath, String hashStoragePath) {
  230 + Map<String, String> storage = parseStorage(hashStoragePath);
  231 + String apache_sha256="";
  232 + File file = null;
  233 + try {
  234 + file = new File(filePath);
  235 + FileInputStream in = new FileInputStream(file);
  236 + apache_sha256 = DigestUtils.sha256Hex(in);
  237 +
  238 + }
  239 + catch (FileNotFoundException ex) {
  240 + }
  241 + catch (IOException ex) {
  242 + }
  243 +
  244 + if(storage.containsKey(file.getPath())) {
  245 + if(storage.get(file.getPath()).equals(apache_sha256)) {
  246 + return true;
  247 + }
  248 + }
  249 + return false;
  250 +
  251 + }
  252 +
  253 + private static Map<String, String> parseStorage(String storage){
  254 + HashMap<String, String> data = new HashMap<>();
  255 +
  256 + String line = null;
  257 +
  258 + try {
  259 +
  260 + BufferedReader bufferedReader =
  261 + new BufferedReader( new FileReader(storage));
  262 +
  263 + while((line = bufferedReader.readLine()) != null) {
  264 + String[] items = line.split(",");
  265 + data.put(items[0], items[1]);
  266 + }
  267 +
  268 + bufferedReader.close();
  269 + }
  270 + catch(FileNotFoundException ex) {
  271 + System.out.println(
  272 + "Unable to open file '" +
  273 + storage + "'");
  274 + }
  275 + catch(IOException ex) {
  276 + System.out.println(
  277 + "Error reading file '"
  278 + + storage + "'");
  279 + }
  280 + return data;
  281 +
  282 + }
225 283  
226 284 public static void main(String args[]) {
227 285 boolean testg=false;
228 286 boolean testv=false;
  287 +
229 288 final File root = new File("files");
  289 +
230 290 search(root);
  291 + System.out.println("Does the file's checksum matches the expected one? " + verifisereHash("files/firstFolder/names.txt", "hashfil.txt"));
  292 + System.out.println("Does the file's checksum matches the expected one? " + verifisereHash("files/secondFolder/phones.txt", "hashfil.txt"));
  293 +
231 294 testg=genSig("test","hashfil.txt","testing","signedhash","pkey");
232 295 testv=verSig("pkey","signedhash","hashfil.txt");
233 296 if(testg && testv) {
234 297  
235 298 System.out.println("Done");
236   - }
237   - else {
  299 + }else{
238 300 System.out.println("Failed to generate signature");
239 301 }
240 302 }
241   -
242   -}
243 303 \ No newline at end of file
  304 + }
... ...