Advertisement
Guest User

omega scuffed

a guest
Sep 11th, 2023
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.91 KB | Source Code | 0 0
  1. import java.math.BigInteger;
  2. import java.nio.charset.StandardCharsets;
  3. import java.security.MessageDigest;
  4.  
  5. public class BlackMagic{
  6.     public static void main(String[] args) throws Exception {
  7.         String[][] collisionFragments = {
  8.             {"Observe! ", "OBSERVE!"},
  9.             {"THIS is also partial hash collision! ", "THIS is also partial SHA256 collision! ", "This is also partial hash collision! ", "This is also partial SHA256 collision! ", "Check the hash of this comment! ", "Check the SHA256 of this comment! ", "Check the hash of THIS comment! ", "Check the SHA256 of THIS comment! "},
  10.             {"Same first ", "Same initial ", "Same starting "},
  11.             {"seven ", "7 "},
  12.             {"Hexadecimal digits", "Hex digits ", "hexadecimal digits", "hex digits", "digits"},
  13.             {". ", "! "},
  14.             {"You can do it too", "It's pretty easy to do", "It's actually pretty easy", "It's pretty easy", "It's quite easy to do", "It's pretty simple" , "It's quite simple", "it's easy", "It's simple"},
  15.             {". ", "! "},
  16.             {"One can simply ", "One may ", "Just ", "Simply "},
  17.             {"write a bunch of message fragments, combine them, ", "write message fragments, combine them, ", "programatically generate millions of strings, ", "programatically generate ~270 million strings, ", "generate ~16^7 candidate tweets, ", "generate a bunch of candidate tweets, ", " generate a bunch of messages, ", "generate millions of candidate tweets, "},
  18.             {"and check for a collision. ", "and check all of them for a collision. ", "and check them for a collision. ", "and search for a collision. ", "then search for a collision. "},
  19.             {"That's how I did it, at least. ", "At least, that's how I did it. ", "That's how I did it. ", "That's one way, at least. ", "That's one way. ", "That's what I did. "},
  20.             {"Dunno what OP did. ", "Dunno what they did. ", "Couldn't tell you what OP did. ", "Couldn't tell you what they did. ", "Don't know what OP did. ", "Don't know what they did. "},
  21.             {"Pastebin link to code below", "Pastebin link to java code below", "Pastebin link to Java code below", "Pastebin link below", "Pastebin link in reply", "Link to code in reply", "Link below", "Link in reply"},
  22.             {". ", "! "},
  23.             {"Be warned, "},
  24.             {"it takes like 10min ", "it takes like ten minutes ", "it takes about ten minutes ", "it takes about 10min "},
  25.             {"to run", "to execute"},
  26.             {".", "!"}
  27.         };
  28.  
  29.         String message = "The SHA256 for this sentence begins with: one, eight, two, a, seven, c and nine.";
  30.  
  31.         recursiveCollisionDetection(collisionFragments, toHex(hash(message)).substring(0, collisionDigits), "", 0);
  32.     }
  33.  
  34.     // Super hacky hash collision search
  35.    
  36.     private static final int collisionDigits = 7;
  37.  
  38.     private static String recursiveCollisionDetection(String[][] collisionFragments, String messageHash, String building, int i) throws Exception {
  39.         if(i == collisionFragments.length){
  40.             byte[] buildingHash = hash(building);
  41.             if(messageHash.equals(toHex(buildingHash).substring(0, collisionDigits))){
  42.                 System.out.println(building + "\n" + toHex(buildingHash));
  43.                 System.exit(0);
  44.             }
  45.         } else {
  46.             for(int j = 0; j < collisionFragments[i].length; j++){
  47.                 recursiveCollisionDetection(collisionFragments, messageHash, building + collisionFragments[i][j], i + 1);
  48.             }
  49.         }
  50.  
  51.         return null;
  52.     }
  53.  
  54.     private static byte[] hash(String msg) throws Exception {
  55.         MessageDigest digest = MessageDigest.getInstance("SHA-256");
  56.         return digest.digest(msg.getBytes(StandardCharsets.UTF_8));
  57.     }
  58.  
  59.     private static String toHex(byte[] bytes) {
  60.         BigInteger bi = new BigInteger(1, bytes);
  61.         return String.format("%0" + (bytes.length << 1) + "X", bi);
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement