Skip to content

Instantly share code, notes, and snippets.

View ssaurel's full-sized avatar

Sylvain Saurel ssaurel

View GitHub Profile
@ssaurel
ssaurel / SameLetters.java
Created January 1, 2020 08:25
sameLetters method for the Anagram Finder tutorial on the SSaurel's Blog
// Method for comparing two strings and returning true if they have same letters
public static boolean sameLetters(String a, String b) {
if (a == null)
return b == null;
if (b == null)
return false;
char[] left = a.toCharArray();
char[] right = b.toCharArray();
@ssaurel
ssaurel / LoadWords.java
Created January 1, 2020 08:15
loadWords method from Anagram Finder tutorial on SSaurel's Blog
// Method for loading words from assets
public static void loadWords(Context context) {
BufferedReader buf = null;
try {
buf = new BufferedReader(new InputStreamReader(context.getAssets().open(DICT)));
String line = null;
// we read file line by line to store words
@ssaurel
ssaurel / MyBlockchain.js
Last active December 18, 2019 15:17
Blockchain Implementation in JS by @ssaurel
var blockchain = new Blockchain(4);
var block1 = blockchain.newBlock("Second Block");
blockchain.addBlock(block1);
var block2 = blockchain.newBlock("Third Block");
blockchain.addBlock(block2);
var block3 = blockchain.newBlock("Fourth Block");
blockchain.addBlock(block3);
console.log("Blockchain Validity: " + blockchain.isBlockchainValid());
@ssaurel
ssaurel / MyBlockchain.js
Last active December 18, 2019 15:07
Blockchain in JS in action by @ssaurel
var blockchain = new Blockchain(4);
var block1 = blockchain.newBlock("Second Block");
blockchain.addBlock(block1);
var block2 = blockchain.newBlock("Third Block");
blockchain.addBlock(block2);
var block3 = blockchain.newBlock("Fourth Block");
blockchain.addBlock(block3);
console.log("Blockchain Validity: " + blockchain.isBlockchainValid());
@ssaurel
ssaurel / Blockchain.js
Last active December 18, 2019 15:16
Blockchain implementation in JS by @ssaurel
class Blockchain {
constructor(difficulty) {
this.difficulty = difficulty;
this.blocks = [];
// Add Genesis Block
var genesisBlock = new Block(0, null, Date.now(), "Genesis block");
genesisBlock.mineBlock(this.difficulty);
this.blocks.push(genesisBlock);
}
}
@ssaurel
ssaurel / Blockchain.js
Created December 18, 2019 14:23
Blockchain for the tutorial on the SSaurel's Blog
class Blockchain {
constructor(difficulty) {
this.difficulty = difficulty;
this.blocks = [];
// Add Genesis Block
var genesisBlock = new Block(0, null, Date.now(), "Genesis block");
genesisBlock.mineBlock(this.difficulty);
this.blocks.push(genesisBlock);
}
}
@ssaurel
ssaurel / Blockchain.js
Created December 18, 2019 14:02
Blockchain in JS for a tutorial on the SSaurel's Blog
class Blockchain {
constructor(difficulty) {
this.difficulty = difficulty;
this.blocks = [];
}
}
@ssaurel
ssaurel / MineBlock.js
Created December 18, 2019 13:33
MineBlock function on the Blockchain in JS made on the SSaurel's Blog
Block.prototype.mineBlock = function(difficulty) {
this.nonce = 0;
var zeros = "0".repeat(difficulty);
while (this.hash.substring(0, difficulty) != zeros) {
this.nonce++;
this.hash = calculateHash(this);
}
};
@ssaurel
ssaurel / calculateHash.js
Created December 18, 2019 13:03
CalculateHash function for a Blockchain in JS on the SSaurel's Blog
function calculateHash(block) {
return sha256(block.index + block.previousHash +
block.timestamp + block.data +
block.nonce);
}
@ssaurel
ssaurel / Block.js
Last active December 18, 2019 13:18
Block for a Blockchain made in JS on the SSaurel's Blog
class Block {
constructor(index, previousHash, timestamp, data) {
this.index = index;
this.previousHash = previousHash;
this.timestamp = timestamp;
this.data = data;
this.nonce = 0;
this.hash = calculateHash(this); // defined later
}
}