From 0c906b701f0bb37ad1b5b21624ce1c5e12144ced Mon Sep 17 00:00:00 2001 From: Willem Date: Wed, 23 Feb 2022 17:11:05 +0100 Subject: [PATCH] Update to water shot with addict code --- .../warpme/hash/WaterDrugs.java | 26 -------- .../warpme/hash/WaterShot.java | 26 ++++++++ .../warpme/hash/WaterShotAddict.java | 65 +++++++++++++++++++ ...rDrugsDriver.java => WaterShotDriver.java} | 6 +- ...{WaterFileHash.java => WaterShotHash.java} | 20 ++++-- .../META-INF/warp-hash/warp-hash-lang.eld | 14 ++-- .../src/resources/META-INF/x4o-drivers.xml | 2 +- .../warpme/hash/WaterShotDriverTest.java | 26 ++++++++ 8 files changed, 142 insertions(+), 43 deletions(-) delete mode 100644 lib-warpme/src/main/love/distributedrebirth/warpme/hash/WaterDrugs.java create mode 100644 lib-warpme/src/main/love/distributedrebirth/warpme/hash/WaterShot.java create mode 100644 lib-warpme/src/main/love/distributedrebirth/warpme/hash/WaterShotAddict.java rename lib-warpme/src/main/love/distributedrebirth/warpme/hash/{WaterDrugsDriver.java => WaterShotDriver.java} (76%) rename lib-warpme/src/main/love/distributedrebirth/warpme/hash/{WaterFileHash.java => WaterShotHash.java} (57%) create mode 100644 lib-warpme/src/test/love/distributedrebirth/warpme/hash/WaterShotDriverTest.java diff --git a/lib-warpme/src/main/love/distributedrebirth/warpme/hash/WaterDrugs.java b/lib-warpme/src/main/love/distributedrebirth/warpme/hash/WaterDrugs.java deleted file mode 100644 index 519340ba..00000000 --- a/lib-warpme/src/main/love/distributedrebirth/warpme/hash/WaterDrugs.java +++ /dev/null @@ -1,26 +0,0 @@ -package love.distributedrebirth.warpme.hash; - -import java.util.ArrayList; -import java.util.List; - -import love.distributedrebirth.bassboonyd.BãßBȍőnAuthorInfoʸᴰ; - -@BãßBȍőnAuthorInfoʸᴰ(name = "willemtsade", copyright = "©Δ∞ 仙上主天") -public class WaterDrugs { - - private List fileHashes = new ArrayList<>(); - - public List getFileHashes() { - return fileHashes; - } - - public void setFileHashes(List fileHashes) { - for (WaterFileHash hash:fileHashes) { - addFileHash(hash); - } - } - - public void addFileHash(WaterFileHash fileHash) { - this.fileHashes.add(fileHash); - } -} diff --git a/lib-warpme/src/main/love/distributedrebirth/warpme/hash/WaterShot.java b/lib-warpme/src/main/love/distributedrebirth/warpme/hash/WaterShot.java new file mode 100644 index 00000000..b8fab613 --- /dev/null +++ b/lib-warpme/src/main/love/distributedrebirth/warpme/hash/WaterShot.java @@ -0,0 +1,26 @@ +package love.distributedrebirth.warpme.hash; + +import java.util.ArrayList; +import java.util.List; + +import love.distributedrebirth.bassboonyd.BãßBȍőnAuthorInfoʸᴰ; + +@BãßBȍőnAuthorInfoʸᴰ(name = "willemtsade", copyright = "©Δ∞ 仙上主天") +public class WaterShot { + + private List shotHashes = new ArrayList<>(); + + public List getShotHashes() { + return shotHashes; + } + + public void setShotHashes(List shotHashes) { + for (WaterShotHash hash:shotHashes) { + addShotHash(hash); + } + } + + public void addShotHash(WaterShotHash shotHash) { + this.shotHashes.add(shotHash); + } +} diff --git a/lib-warpme/src/main/love/distributedrebirth/warpme/hash/WaterShotAddict.java b/lib-warpme/src/main/love/distributedrebirth/warpme/hash/WaterShotAddict.java new file mode 100644 index 00000000..d8f956b6 --- /dev/null +++ b/lib-warpme/src/main/love/distributedrebirth/warpme/hash/WaterShotAddict.java @@ -0,0 +1,65 @@ +package love.distributedrebirth.warpme.hash; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.file.Files; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +import org.x4o.xml.io.X4OConnectionException; +import org.xml.sax.SAXException; + +import love.distributedrebirth.bassboonyd.BãßBȍőnAuthorInfoʸᴰ; +import love.distributedrebirth.warpme.Warpᵐᵉ; + +@BãßBȍőnAuthorInfoʸᴰ(name = "willemtsade", copyright = "©Δ∞ 仙上主天") +public class WaterShotAddict { + + public boolean validateWarpChainLink(File folder) throws FileNotFoundException, X4OConnectionException, SAXException, IOException, NoSuchAlgorithmException { + File fileWarpHash = new File(folder, Warpᵐᵉ.WARP_HASH); + if (!fileWarpHash.exists()) { + return false; + } + WaterShot waterShot = WaterShotDriver.newInstance().createReader().readFile(fileWarpHash); + for (WaterShotHash hash:waterShot.getShotHashes()) { + File checkFile = new File(folder, hash.getFile()); + if (!checkFile.exists()) { + return false; + } + String fileHex = calcHashInHex(checkFile); + if (!fileHex.equals(hash.getHex())) { + return false; + } + } + return true; + } + + public void updateWarpHashes(File folder) throws FileNotFoundException, X4OConnectionException, SAXException, IOException, NoSuchAlgorithmException { + File fileWarpHash = new File(folder, Warpᵐᵉ.WARP_HASH); + WaterShot waterShot = WaterShotDriver.newInstance().createReader().readFile(fileWarpHash); + for (WaterShotHash hash:waterShot.getShotHashes()) { + hash.setHex(calcHashInHex(new File(folder, hash.getFile()))); + } + WaterShotDriver.newInstance().createWriter().writeFile(waterShot, fileWarpHash); + } + + private String calcHashInHex(File file) throws IOException, NoSuchAlgorithmException { + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + byte[] bytes = Files.readAllBytes(file.toPath()); + byte[] encodedhash = digest.digest(bytes); + return bytesToHex(encodedhash); + } + + private String bytesToHex(byte[] hash) { + StringBuilder hexString = new StringBuilder(2 * hash.length); + for (int i = 0; i < hash.length; i++) { + String hex = Integer.toHexString(0xff & hash[i]); + if(hex.length() == 1) { + hexString.append('0'); + } + hexString.append(hex); + } + return hexString.toString(); + } +} diff --git a/lib-warpme/src/main/love/distributedrebirth/warpme/hash/WaterDrugsDriver.java b/lib-warpme/src/main/love/distributedrebirth/warpme/hash/WaterShotDriver.java similarity index 76% rename from lib-warpme/src/main/love/distributedrebirth/warpme/hash/WaterDrugsDriver.java rename to lib-warpme/src/main/love/distributedrebirth/warpme/hash/WaterShotDriver.java index 8d45ef5d..d0d0b19a 100644 --- a/lib-warpme/src/main/love/distributedrebirth/warpme/hash/WaterDrugsDriver.java +++ b/lib-warpme/src/main/love/distributedrebirth/warpme/hash/WaterShotDriver.java @@ -6,7 +6,7 @@ import org.x4o.xml.X4ODriverManager; import love.distributedrebirth.bassboonyd.BãßBȍőnAuthorInfoʸᴰ; @BãßBȍőnAuthorInfoʸᴰ(name = "willemtsade", copyright = "©Δ∞ 仙上主天") -public class WaterDrugsDriver extends X4ODriver { +public class WaterShotDriver extends X4ODriver { static final public String LANGUAGE_NAME = "warp-hash"; static final public String[] LANGUAGE_VERSIONS = new String[]{X4ODriver.DEFAULT_LANGUAGE_VERSION}; @@ -21,7 +21,7 @@ public class WaterDrugsDriver extends X4ODriver { return LANGUAGE_VERSIONS; } - static public WaterDrugsDriver newInstance() { - return (WaterDrugsDriver)X4ODriverManager.getX4ODriver(LANGUAGE_NAME); + static public WaterShotDriver newInstance() { + return (WaterShotDriver)X4ODriverManager.getX4ODriver(LANGUAGE_NAME); } } diff --git a/lib-warpme/src/main/love/distributedrebirth/warpme/hash/WaterFileHash.java b/lib-warpme/src/main/love/distributedrebirth/warpme/hash/WaterShotHash.java similarity index 57% rename from lib-warpme/src/main/love/distributedrebirth/warpme/hash/WaterFileHash.java rename to lib-warpme/src/main/love/distributedrebirth/warpme/hash/WaterShotHash.java index 789ac2f8..d5662b9e 100644 --- a/lib-warpme/src/main/love/distributedrebirth/warpme/hash/WaterFileHash.java +++ b/lib-warpme/src/main/love/distributedrebirth/warpme/hash/WaterShotHash.java @@ -3,10 +3,18 @@ package love.distributedrebirth.warpme.hash; import love.distributedrebirth.bassboonyd.BãßBȍőnAuthorInfoʸᴰ; @BãßBȍőnAuthorInfoʸᴰ(name = "willemtsade", copyright = "©Δ∞ 仙上主天") -public class WaterFileHash { +public class WaterShotHash { private String file; - private String hash; + private String hex; + + public WaterShotHash() { + } + + public WaterShotHash(String file, String hex) { + setFile(file); + setHex(hex); + } public String getFile() { return file; @@ -16,11 +24,11 @@ public class WaterFileHash { this.file = file; } - public String getHash() { - return hash; + public String getHex() { + return hex; } - public void setHash(String hash) { - this.hash = hash; + public void setHex(String hex) { + this.hex = hex; } } diff --git a/lib-warpme/src/resources/META-INF/warp-hash/warp-hash-lang.eld b/lib-warpme/src/resources/META-INF/warp-hash/warp-hash-lang.eld index da41a662..044e1558 100644 --- a/lib-warpme/src/resources/META-INF/warp-hash/warp-hash-lang.eld +++ b/lib-warpme/src/resources/META-INF/warp-hash/warp-hash-lang.eld @@ -7,10 +7,10 @@ providerHost="warp-hash.x4o.distributedrebirth.love" providerName="WarpHashᵐᵉ" id="warp-hash-module"> - + - + + objectClass="love.distributedrebirth.warpme.hash.WaterShotHash" /> \ No newline at end of file diff --git a/lib-warpme/src/resources/META-INF/x4o-drivers.xml b/lib-warpme/src/resources/META-INF/x4o-drivers.xml index c6b5b9b3..17e9234b 100644 --- a/lib-warpme/src/resources/META-INF/x4o-drivers.xml +++ b/lib-warpme/src/resources/META-INF/x4o-drivers.xml @@ -5,6 +5,6 @@ xsi:schemaLocation="http://language.x4o.org/xml/ns/drivers http://language.x4o.org/xml/ns/drivers-1.0.xsd" > - + diff --git a/lib-warpme/src/test/love/distributedrebirth/warpme/hash/WaterShotDriverTest.java b/lib-warpme/src/test/love/distributedrebirth/warpme/hash/WaterShotDriverTest.java new file mode 100644 index 00000000..09f25151 --- /dev/null +++ b/lib-warpme/src/test/love/distributedrebirth/warpme/hash/WaterShotDriverTest.java @@ -0,0 +1,26 @@ +package love.distributedrebirth.warpme.hash; + +import java.io.IOException; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.x4o.xml.io.X4OConnectionException; +import org.xml.sax.SAXException; + +import love.distributedrebirth.bassboonyd.BãßBȍőnAuthorInfoʸᴰ; + +@BãßBȍőnAuthorInfoʸᴰ(name = "willemtsade", copyright = "©Δ∞ 仙上主天") +public class WaterShotDriverTest { + + + @Test + public void testWaterShot() throws X4OConnectionException, SAXException, IOException { + + WaterShot drugs = new WaterShot(); + drugs.addShotHash(new WaterShotHash("test.txt", "234SDFSDF234234SDFSDF234")); + drugs.addShotHash(new WaterShotHash("test2.txt", "ASDFYYF234234SDFSDFASDFS")); + + Assertions.assertNotNull(drugs); + //WaterShotDriver.newInstance().createWriter().writeFile(drugs, new File("build/output.xml")); + } +} \ No newline at end of file