From 4404d25f2046ed9276a29ecb4c907b0997787553 Mon Sep 17 00:00:00 2001 From: chubtub <43381989+chubtub@users.noreply.github.com> Date: Fri, 19 Jun 2020 07:24:11 -0400 Subject: [PATCH] Revert "Revert changes to HashSwid class" This reverts commit d850d69b1e8b14367b25c075dd8f61b30e558c02. --- .../main/java/hirs/swid/SwidTagGateway.java | 24 ++++--------- .../main/java/hirs/swid/utils/HashSwid.java | 36 ++++--------------- 2 files changed, 14 insertions(+), 46 deletions(-) diff --git a/tools/tcg_rim_tool/src/main/java/hirs/swid/SwidTagGateway.java b/tools/tcg_rim_tool/src/main/java/hirs/swid/SwidTagGateway.java index 17adfd94..f57e875d 100644 --- a/tools/tcg_rim_tool/src/main/java/hirs/swid/SwidTagGateway.java +++ b/tools/tcg_rim_tool/src/main/java/hirs/swid/SwidTagGateway.java @@ -428,11 +428,7 @@ public class SwidTagGateway { File rimEventLogFile = new File(rimEventLog); file.setSize(new BigInteger(Long.toString(rimEventLogFile.length()))); Map attributes = file.getOtherAttributes(); - try { - addNonNullAttribute(attributes, _SHA256_HASH, HashSwid.getHashValue(Files.readAllBytes(Paths.get(rimEventLog)))); - } catch (IOException e) { - System.out.println("Error hashing support RIM: " + e.getMessage()); - } + addNonNullAttribute(attributes, _SHA256_HASH, HashSwid.get256Hash(rimEventLog)); return file; } @@ -443,19 +439,13 @@ public class SwidTagGateway { private boolean validateFile(Element file) { String filepath = file.getAttribute(SwidTagConstants.NAME); System.out.println("Support rim found at " + filepath); - byte[] bytes = new byte[]{}; - try { - bytes = Files.readAllBytes(Paths.get(filepath)); - } catch (IOException e) { - System.out.println("Error while hashing support RIM to verify: " + e.getMessage()); + if (HashSwid.get256Hash(filepath).equals(file.getAttribute(_SHA256_HASH.getPrefix() + ":" + _SHA256_HASH.getLocalPart()))) { + System.out.println("Support RIM hash verified!"); + return true; + } else { + System.out.println("Support RIM hash does not match Base RIM!"); + return false; } - if (HashSwid.getHashValue(bytes).equals(file.getAttribute(_SHA256_HASH.getPrefix() + ":" + _SHA256_HASH.getLocalPart()))) { - System.out.println("Support RIM hash verified!"); - return true; - } else { - System.out.println("Support RIM hash does not match Base RIM!"); - return false; - } } /** diff --git a/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/HashSwid.java b/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/HashSwid.java index 62995ad5..1b33f6bf 100644 --- a/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/HashSwid.java +++ b/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/HashSwid.java @@ -52,6 +52,10 @@ public class HashSwid { * This method creates the hash based on the provided algorithm and salt * only accessible through helper methods. * + * This method assumes an input file that is small enough to read in its + * entirety. Large files should be handled similarly to the public static + * getHashValue() below. + * * @param filepath file contents to hash * @param salt random value to make the hash stronger * @param sha the algorithm to use for the hash @@ -61,7 +65,7 @@ public class HashSwid { String resultString = null; try { MessageDigest md = MessageDigest.getInstance(sha); - byte[] bytes = md.digest(filepath.getBytes(ENCODING)); + byte[] bytes = md.digest(Files.readAllBytes(Paths.get(filepath))); StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { @@ -70,34 +74,8 @@ public class HashSwid { resultString = sb.toString(); } catch (UnsupportedEncodingException | NoSuchAlgorithmException grex) { System.out.println(grex.getMessage()); - } - - return resultString; - } - - /** - * This method creates a hash based on the provided algorithm and salt - * only accessible through helper methods. - * - * This method assumes an input file that is small enough to read in its - * entirety. Large files should be handled similarly to the public static - * getHashValue() below. - * - * This method is also largely redundant and should be refactored after 2.0. - */ - public static String getHashValue(byte[] content) { - String resultString = null; - try { - MessageDigest md = MessageDigest.getInstance(SHA256); - byte[] bytes = md.digest(content); - StringBuilder sb = new StringBuilder(); - - for (int i = 0; i < bytes.length; i++) { - sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1)); - } - resultString = sb.toString(); - } catch (NoSuchAlgorithmException grex) { - System.out.println(grex.getMessage()); + } catch (IOException e) { + System.out.println("Error reading in file to hash: " + e.getMessage()); } return resultString;