This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void mine(int difficulty) throws NoSuchAlgorithmException { | |
String pre = ""; | |
for (; pre.length() < difficulty; pre += "0"); | |
while(!getHash().startsWith(pre)) | |
nonce++; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5FECEB66FFC86F38D952786C6D696C79C2DBC239DD4E91B46729D73A27FB57E9 1527703318986 0 Hello | |
E5A6134694C22742C88639C8E3442BDF19FB7843F829B2351B24CB0CCC9AA917 1527703318986 0 World | |
759F215F98C2374696552663280BBC90378741DBD36B067A6DF0FAA50DD38769 1527703318986 0 Whatever | |
Is valid chain : true | |
Is valid chain : false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
try { | |
var blockChain = new BlockChain(); | |
Block block1 = new Block("Hello"); | |
Block block2 = new Block("World"); | |
Block block3 = new Block("Whatever"); | |
blockChain.addBlock(block1); | |
blockChain.addBlock(block2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5FECEB66FFC86F38D952786C6D696C79C2DBC239DD4E91B46729D73A27FB57E9 1527703318986 0 Hello | |
E5A6134694C22742C88639C8E3442BDF19FB7843F829B2351B24CB0CCC9AA917 1527703318986 0 World | |
759F215F98C2374696552663280BBC90378741DBD36B067A6DF0FAA50DD38769 1527703318986 0 Whatever | |
Is valid chain : true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String[] args) { | |
try { | |
var blockChain = new BlockChain(); | |
Block block1 = new Block("Hello"); | |
Block block2 = new Block("World"); | |
Block block3 = new Block("Whatever"); | |
blockChain.addBlock(block1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.security.NoSuchAlgorithmException; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class BlockChain { | |
public List<Block> chain; | |
public BlockChain() { | |
chain = new ArrayList<>(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Block { | |
public long nonce; | |
private long timestamp; | |
public String transactionData; | |
public String previousBlockHash; | |
public Block(String transactionData) { | |
nonce = 0; | |
timestamp = System.currentTimeMillis(); | |
this.transactionData = transactionData; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.nio.charset.StandardCharsets; | |
public class CryptoHelper { | |
public static String sha256(String text) throws NoSuchAlgorithmException { | |
MessageDigest digest = MessageDigest.getInstance("SHA-256"); | |
digest.update(text.getBytes(StandardCharsets.UTF_8)); | |
byte[] hash = digest.digest(); | |
StringBuffer sb = new StringBuffer(); | |
for (int i = 0; i < hash.length; i++) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BD5370EB390348AA71C25C65E700E36E81BA370C665C6398BAEF7C8843C0A1C2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ java HashBreaker | |
Nonce Finder | |
Message : Block 4152799093110700000277418AFAF4F19430EDE51B573FAE4C3DCB13B20352A43778802DF0E12F | |
Difficulty : 5 | |
Calculating... | |
Valid Nonce : 691412 | |
Hash : 00000FDAD0C18DB1C15980029F94D6C37DE8B316FF531B9E7ACA233EC95545CE |