算法2026-04-25·8 分钟
算法知识库:区块链基础算法实现
JavaScript/TypeScript 实现区块链基础算法,如哈希函数、工作量证明、默克尔树等。
区块链基础算法实现
1. SHA-256 哈希函数
ts
class SHA256 {
private static K = [
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
];
static hash(message: string): string {
const msg = this.preprocess(message);
const chunks = this.chunkMessage(msg);
let h0 = 0x6a09e667;
let h1 = 0xbb67ae85;
let h2 = 0x3c6ef372;
let h3 = 0xa54ff53a;
let h4 = 0x510e527f;
let h5 = 0x9b05688c;
let h6 = 0x1f83d9ab;
let h7 = 0x5be0cd19;
for (const chunk of chunks) {
const w = this.createMessageSchedule(chunk);
let [a, b, c, d, e, f, g, h] = [h0, h1, h2, h3, h4, h5, h6, h7];
for (let i = 0; i < 64; i += 1) {
const s1 = this.rightRotate(e, 6) ^ this.rightRotate(e, 11) ^ this.rightRotate(e, 25);
const ch = (e & f) ^ (~e & g);
const temp1 = (h + s1 + ch + this.K[i] + w[i]) >>> 0;
const s0 = this.rightRotate(a, 2) ^ this.rightRotate(a, 13) ^ this.rightRotate(a, 22);
const maj = (a & b) ^ (a & c) ^ (b & c);
const temp2 = (s0 + maj) >>> 0;
h = g;
g = f;
f = e;
e = (d + temp1) >>> 0;
d = c;
c = b;
b = a;
a = (temp1 + temp2) >>> 0;
}
h0 = (h0 + a) >>> 0;
h1 = (h1 + b) >>> 0;
h2 = (h2 + c) >>> 0;
h3 = (h3 + d) >>> 0;
h4 = (h4 + e) >>> 0;
h5 = (h5 + f) >>> 0;
h6 = (h6 + g) >>> 0;
h7 = (h7 + h) >>> 0;
}
return [h0, h1, h2, h3, h4, h5, h6, h7].map((h) => h.toString(16).padStart(8, '0')).join('');
}
private static preprocess(message: string): number[] {
const bytes = [];
for (let i = 0; i < message.length; i += 1) {
const code = message.charCodeAt(i);
if (code < 0x80) {
bytes.push(code);
} else if (code < 0x800) {
bytes.push(0xc0 | (code >> 6));
bytes.push(0x80 | (code & 0x3f));
} else {
bytes.push(0xe0 | (code >> 12));
bytes.push(0x80 | ((code >> 6) & 0x3f));
bytes.push(0x80 | (code & 0x3f));
}
}
const msgLen = bytes.length;
bytes.push(0x80);
while (bytes.length % 64 !== 56) {
bytes.push(0);
}
const lengthBits = msgLen * 8;
for (let i = 7; i >= 0; i -= 1) {
bytes.push((lengthBits >>> (i * 8)) & 0xff);
}
return bytes;
}
private static chunkMessage(bytes: number[]): number[][] {
const chunks = [];
for (let i = 0; i < bytes.length; i += 64) {
chunks.push(bytes.slice(i, i + 64));
}
return chunks;
}
private static createMessageSchedule(chunk: number[]): number[] {
const w = [];
for (let i = 0; i < 16; i += 1) {
w[i] = (chunk[i * 4] << 24) | (chunk[i * 4 + 1] << 16) | (chunk[i * 4 + 2] << 8) | chunk[i * 4 + 3];
}
for (let i = 16; i < 64; i += 1) {
const s0 = this.rightRotate(w[i - 15], 7) ^ this.rightRotate(w[i - 15], 18) ^ (w[i - 15] >>> 3);
const s1 = this.rightRotate(w[i - 2], 17) ^ this.rightRotate(w[i - 2], 19) ^ (w[i - 2] >>> 10);
w[i] = (w[i - 16] + s0 + w[i - 7] + s1) >>> 0;
}
return w;
}
private static rightRotate(value: number, amount: number): number {
return ((value >>> amount) | (value << (32 - amount))) >>> 0;
}
}2. 工作量证明 (Proof of Work)
ts
class ProofOfWork {
private difficulty: number;
constructor(difficulty: number = 4) {
this.difficulty = difficulty;
}
mine(blockData: string): { nonce: number; hash: string } {
let nonce = 0;
let hash = '';
do {
nonce += 1;
hash = SHA256.hash(blockData + nonce);
} while (!this.isValidHash(hash));
return { nonce, hash };
}
isValidHash(hash: string): boolean {
return hash.startsWith('0'.repeat(this.difficulty));
}
}3. 默克尔树
ts
class MerkleTree {
private leaves: string[];
private tree: string[][];
constructor(leaves: string[]) {
this.leaves = leaves.map((leaf) => SHA256.hash(leaf));
this.buildTree();
}
private buildTree(): void {
this.tree = [this.leaves];
while (this.tree[this.tree.length - 1].length > 1) {
const currentLevel = this.tree[this.tree.length - 1];
const nextLevel: string[] = [];
for (let i = 0; i < currentLevel.length; i += 2) {
const left = currentLevel[i];
const right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left;
nextLevel.push(SHA256.hash(left + right));
}
this.tree.push(nextLevel);
}
}
getRoot(): string {
return this.tree[this.tree.length - 1][0];
}
getProof(index: number): { hash: string; isLeft: boolean }[] {
const proof: { hash: string; isLeft: boolean }[] = [];
let currentIndex = index;
for (let level = 0; level < this.tree.length - 1; level += 1) {
const isLeft = currentIndex % 2 === 0;
const siblingIndex = isLeft ? currentIndex + 1 : currentIndex - 1;
if (siblingIndex < this.tree[level].length) {
proof.push({
hash: this.tree[level][siblingIndex],
isLeft: !isLeft,
});
}
currentIndex = Math.floor(currentIndex / 2);
}
return proof;
}
verifyProof(leaf: string, proof: { hash: string; isLeft: boolean }[], root: string): boolean {
let hash = SHA256.hash(leaf);
for (const { hash: siblingHash, isLeft } of proof) {
if (isLeft) {
hash = SHA256.hash(siblingHash + hash);
} else {
hash = SHA256.hash(hash + siblingHash);
}
}
return hash === root;
}
}4. 区块链块
ts
interface BlockData {
index: number;
timestamp: number;
data: string;
previousHash: string;
}
class Block {
index: number;
timestamp: number;
data: string;
previousHash: string;
hash: string;
nonce: number;
constructor(data: BlockData) {
this.index = data.index;
this.timestamp = data.timestamp;
this.data = data.data;
this.previousHash = data.previousHash;
this.nonce = 0;
this.hash = this.calculateHash();
}
calculateHash(): string {
return SHA256.hash(this.index + this.timestamp + this.data + this.previousHash + this.nonce);
}
mineBlock(difficulty: number): void {
const pow = new ProofOfWork(difficulty);
const result = pow.mine(this.index + this.timestamp + this.data + this.previousHash);
this.nonce = result.nonce;
this.hash = result.hash;
}
}5. 区块链
ts
class Blockchain {
private chain: Block[];
private difficulty: number;
constructor(difficulty: number = 4) {
this.chain = [this.createGenesisBlock()];
this.difficulty = difficulty;
}
private createGenesisBlock(): Block {
return new Block({
index: 0,
timestamp: Date.now(),
data: 'Genesis Block',
previousHash: '0',
});
}
getLatestBlock(): Block {
return this.chain[this.chain.length - 1];
}
addBlock(data: string): void {
const newBlock = new Block({
index: this.getLatestBlock().index + 1,
timestamp: Date.now(),
data,
previousHash: this.getLatestBlock().hash,
});
newBlock.mineBlock(this.difficulty);
this.chain.push(newBlock);
}
isChainValid(): boolean {
for (let i = 1; i < this.chain.length; i += 1) {
const currentBlock = this.chain[i];
const previousBlock = this.chain[i - 1];
if (currentBlock.hash !== currentBlock.calculateHash()) {
return false;
}
if (currentBlock.previousHash !== previousBlock.hash) {
return false;
}
}
return true;
}
}6. 实现要点
- SHA-256 提供抗碰撞哈希。
- 工作量证明确保计算难度。
- 默克尔树提供高效验证。
- 区块链通过哈希链接保证不可变性。
算法区块链JavaScript