Skip to content

Instantly share code, notes, and snippets.

Avatar
🖖
live long and prosper

Ravi Teja cyberpirate92

🖖
live long and prosper
View GitHub Profile
View Block.java
public void mine(int difficulty) throws NoSuchAlgorithmException {
String pre = "";
for (; pre.length() < difficulty; pre += "0");
while(!getHash().startsWith(pre))
nonce++;
}
View output.txt
5FECEB66FFC86F38D952786C6D696C79C2DBC239DD4E91B46729D73A27FB57E9 1527703318986 0 Hello
E5A6134694C22742C88639C8E3442BDF19FB7843F829B2351B24CB0CCC9AA917 1527703318986 0 World
759F215F98C2374696552663280BBC90378741DBD36B067A6DF0FAA50DD38769 1527703318986 0 Whatever
Is valid chain : true
Is valid chain : false
View Main.java
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);
View execution.txt
5FECEB66FFC86F38D952786C6D696C79C2DBC239DD4E91B46729D73A27FB57E9 1527703318986 0 Hello
E5A6134694C22742C88639C8E3442BDF19FB7843F829B2351B24CB0CCC9AA917 1527703318986 0 World
759F215F98C2374696552663280BBC90378741DBD36B067A6DF0FAA50DD38769 1527703318986 0 Whatever
Is valid chain : true
View Main.java
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);
View BlockChain.java
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
public class BlockChain {
public List<Block> chain;
public BlockChain() {
chain = new ArrayList<>();
}
View Block.java
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;
View CryptoHelper.java
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++)
View sha-hash.txt
BD5370EB390348AA71C25C65E700E36E81BA370C665C6398BAEF7C8843C0A1C2
View modified-chain2.txt
$ java HashBreaker
Nonce Finder
Message : Block 4152799093110700000277418AFAF4F19430EDE51B573FAE4C3DCB13B20352A43778802DF0E12F
Difficulty : 5
Calculating...
Valid Nonce : 691412
Hash : 00000FDAD0C18DB1C15980029F94D6C37DE8B316FF531B9E7ACA233EC95545CE
You can’t perform that action at this time.