Repository: CryptoKass/NoobChain-Tutorial-Part-1
Branch: master
Commit: 2fb61b75649e
Files: 5
Total size: 5.9 KB
Directory structure:
gitextract_d2jvv62y/
├── LICENSE
├── README.md
└── src/
└── noobchain/
├── Block.java
├── NoobChain.java
└── StringUtil.java
================================================
FILE CONTENTS
================================================
================================================
FILE: LICENSE
================================================
MIT License
Copyright (c) 2017 Kassius
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: README.md
================================================
# NoobChain-Tutorial-Part-1
A Simple Java Blockchain for educational purposes.
This is for https://medium.com/programmers-blockchain/create-simple-blockchain-java-tutorial-from-scratch-6eeed3cb03fa tutorial.
*If you have any other questions you can message me on the [Blockchain Developers Club](https://discord.gg/ZsyQqyk) discord server.*
# Dependencies: You will need to import GSON:
- gson [gson-2.8.2.jar](http://central.maven.org/maven2/com/google/code/gson/gson/2.8.2/gson-2.8.2.jar)
# Java Version:
- JDK1.8.0_77
contact: kassCrypto@gmail.com
================================================
FILE: src/noobchain/Block.java
================================================
package noobchain;
import java.util.Date;
public class Block {
public String hash;
public String previousHash;
private String data; //our data will be a simple message.
private long timeStamp; //as number of milliseconds since 1/1/1970.
private int nonce;
//Block Constructor.
public Block(String data,String previousHash ) {
this.data = data;
this.previousHash = previousHash;
this.timeStamp = new Date().getTime();
this.hash = calculateHash(); //Making sure we do this after we set the other values.
}
//Calculate new hash based on blocks contents
public String calculateHash() {
String calculatedhash = StringUtil.applySha256(
previousHash +
Long.toString(timeStamp) +
Integer.toString(nonce) +
data
);
return calculatedhash;
}
//Increases nonce value until hash target is reached.
public void mineBlock(int difficulty) {
String target = StringUtil.getDificultyString(difficulty); //Create a string with difficulty * "0"
while(!hash.substring( 0, difficulty).equals(target)) {
nonce ++;
hash = calculateHash();
}
System.out.println("Block Mined!!! : " + hash);
}
}
================================================
FILE: src/noobchain/NoobChain.java
================================================
package noobchain;
import java.util.ArrayList;
import com.google.gson.GsonBuilder;
public class NoobChain {
public static ArrayList<Block> blockchain = new ArrayList<Block>();
public static int difficulty = 5;
public static void main(String[] args) {
//add our blocks to the blockchain ArrayList:
System.out.println("Trying to Mine block 1... ");
addBlock(new Block("Hi im the first block", "0"));
System.out.println("Trying to Mine block 2... ");
addBlock(new Block("Yo im the second block",blockchain.get(blockchain.size()-1).hash));
System.out.println("Trying to Mine block 3... ");
addBlock(new Block("Hey im the third block",blockchain.get(blockchain.size()-1).hash));
System.out.println("\nBlockchain is Valid: " + isChainValid());
String blockchainJson = StringUtil.getJson(blockchain);
System.out.println("\nThe block chain: ");
System.out.println(blockchainJson);
}
public static Boolean isChainValid() {
Block currentBlock;
Block previousBlock;
String hashTarget = new String(new char[difficulty]).replace('\0', '0');
//loop through blockchain to check hashes:
for(int i=1; i < blockchain.size(); i++) {
currentBlock = blockchain.get(i);
previousBlock = blockchain.get(i-1);
//compare registered hash and calculated hash:
if(!currentBlock.hash.equals(currentBlock.calculateHash()) ){
System.out.println("Current Hashes not equal");
return false;
}
//compare previous hash and registered previous hash
if(!previousBlock.hash.equals(currentBlock.previousHash) ) {
System.out.println("Previous Hashes not equal");
return false;
}
//check if hash is solved
if(!currentBlock.hash.substring( 0, difficulty).equals(hashTarget)) {
System.out.println("This block hasn't been mined");
return false;
}
}
return true;
}
public static void addBlock(Block newBlock) {
newBlock.mineBlock(difficulty);
blockchain.add(newBlock);
}
}
================================================
FILE: src/noobchain/StringUtil.java
================================================
package noobchain;
import java.security.MessageDigest;
import com.google.gson.GsonBuilder;
public class StringUtil {
//Applies Sha256 to a string and returns the result.
public static String applySha256(String input){
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
//Applies sha256 to our input,
byte[] hash = digest.digest(input.getBytes("UTF-8"));
StringBuffer hexString = new StringBuffer(); // This will contain hash as hexidecimal
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();
}
catch(Exception e) {
throw new RuntimeException(e);
}
}
//Short hand helper to turn Object into a json string
public static String getJson(Object o) {
return new GsonBuilder().setPrettyPrinting().create().toJson(o);
}
//Returns difficulty string target, to compare to hash. eg difficulty of 5 will return "00000"
public static String getDificultyString(int difficulty) {
return new String(new char[difficulty]).replace('\0', '0');
}
}
gitextract_d2jvv62y/
├── LICENSE
├── README.md
└── src/
└── noobchain/
├── Block.java
├── NoobChain.java
└── StringUtil.java
SYMBOL INDEX (12 symbols across 3 files)
FILE: src/noobchain/Block.java
class Block (line 5) | public class Block {
method Block (line 14) | public Block(String data,String previousHash ) {
method calculateHash (line 23) | public String calculateHash() {
method mineBlock (line 34) | public void mineBlock(int difficulty) {
FILE: src/noobchain/NoobChain.java
class NoobChain (line 5) | public class NoobChain {
method main (line 10) | public static void main(String[] args) {
method isChainValid (line 29) | public static Boolean isChainValid() {
method addBlock (line 58) | public static void addBlock(Block newBlock) {
FILE: src/noobchain/StringUtil.java
class StringUtil (line 6) | public class StringUtil {
method applySha256 (line 9) | public static String applySha256(String input){
method getJson (line 31) | public static String getJson(Object o) {
method getDificultyString (line 36) | public static String getDificultyString(int difficulty) {
Condensed preview — 5 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (7K chars).
[
{
"path": "LICENSE",
"chars": 1065,
"preview": "MIT License\n\nCopyright (c) 2017 Kassius \n\nPermission is hereby granted, free of charge, to any person obtaining a copy\no"
},
{
"path": "README.md",
"chars": 556,
"preview": "# NoobChain-Tutorial-Part-1\nA Simple Java Blockchain for educational purposes. \n\nThis is for https://medium.com/programm"
},
{
"path": "src/noobchain/Block.java",
"chars": 1193,
"preview": "package noobchain;\r\n\r\nimport java.util.Date;\r\n\r\npublic class Block {\r\n\t\r\n\tpublic String hash;\r\n\tpublic String previousHa"
},
{
"path": "src/noobchain/NoobChain.java",
"chars": 2029,
"preview": "package noobchain;\r\nimport java.util.ArrayList;\r\nimport com.google.gson.GsonBuilder;\r\n\r\npublic class NoobChain {\r\n\t\r\n\tpu"
},
{
"path": "src/noobchain/StringUtil.java",
"chars": 1219,
"preview": "package noobchain;\r\nimport java.security.MessageDigest;\r\n\r\nimport com.google.gson.GsonBuilder;\r\n\r\npublic class StringUti"
}
]
About this extraction
This page contains the full source code of the CryptoKass/NoobChain-Tutorial-Part-1 GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 5 files (5.9 KB), approximately 1.6k tokens, and a symbol index with 12 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.