算法2026-05-15·15 分钟
算法知识库:密码学算法实现
JavaScript/TypeScript 实现基础密码学算法,如RSA、AES、SHA哈希等。
密码学算法实现
1. RSA 加密算法
ts
class RSA {
private p: bigint;
private q: bigint;
private n: bigint;
private phi: bigint;
private e: bigint;
private d: bigint;
constructor() {
this.generateKeys();
}
// 生成密钥对
private generateKeys(): void {
// 选择两个大质数
this.p = this.generatePrime(512);
this.q = this.generatePrime(512);
// 计算模数 n = p * q
this.n = this.p * this.q;
// 计算欧拉函数 φ(n) = (p-1) * (q-1)
this.phi = (this.p - 1n) * (this.q - 1n);
// 选择公钥指数 e (通常为 65537)
this.e = 65537n;
// 计算私钥指数 d,使得 (d * e) ≡ 1 (mod φ(n))
this.d = this.modInverse(this.e, this.phi);
}
// 生成大质数 (简化版,实际应用中应使用更强的质数生成算法)
private generatePrime(bits: number): bigint {
while (true) {
const candidate = this.randomBigInt(bits);
if (this.isPrime(candidate)) {
return candidate;
}
}
}
// 生成随机大整数
private randomBigInt(bits: number): bigint {
const bytes = Math.ceil(bits / 8);
const array = new Uint8Array(bytes);
crypto.getRandomValues(array);
array[0] |= 0x80; // 确保最高位为1
array[bytes - 1] |= 0x01; // 确保最低位为1 (奇数)
return BigInt(
'0x' +
Array.from(array)
.map((b) => b.toString(16).padStart(2, '0'))
.join(''),
);
}
// 简单质数测试 (实际应用中应使用 Miller-Rabin 测试)
private isPrime(n: bigint): boolean {
if (n < 2n) return false;
if (n === 2n || n === 3n) return true;
if (n % 2n === 0n || n % 3n === 0n) return false;
// 试除法
for (let i = 5n; i * i <= n; i += 6n) {
if (n % i === 0n || n % (i + 2n) === 0n) return false;
}
return true;
}
// 扩展欧几里得算法计算模逆
private modInverse(a: bigint, m: bigint): bigint {
let m0 = m;
let y = 0n;
let x = 1n;
if (m === 1n) return 0n;
while (a > 1n) {
const q = a / m;
let t = m;
m = a % m;
a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0n) x += m0;
return x;
}
// 快速模幂运算
private modPow(base: bigint, exponent: bigint, modulus: bigint): bigint {
let result = 1n;
base = base % modulus;
while (exponent > 0n) {
if (exponent % 2n === 1n) {
result = (result * base) % modulus;
}
base = (base * base) % modulus;
exponent /= 2n;
}
return result;
}
// 加密
encrypt(message: bigint): bigint {
return this.modPow(message, this.e, this.n);
}
// 解密
decrypt(ciphertext: bigint): bigint {
return this.modPow(ciphertext, this.d, this.n);
}
// 获取公钥
getPublicKey(): { n: bigint; e: bigint } {
return { n: this.n, e: this.e };
}
// 获取私钥
getPrivateKey(): { n: bigint; d: bigint } {
return { n: this.n, d: this.d };
}
// 字符串转大整数
static stringToBigInt(str: string): bigint {
const encoder = new TextEncoder();
const bytes = encoder.encode(str);
let result = 0n;
for (const byte of bytes) {
result = (result << 8n) + BigInt(byte);
}
return result;
}
// 大整数转字符串
static bigIntToString(num: bigint): string {
const bytes: number[] = [];
let temp = num;
while (temp > 0n) {
bytes.unshift(Number(temp & 0xffn));
temp >>= 8n;
}
const decoder = new TextDecoder();
return decoder.decode(new Uint8Array(bytes));
}
}
// 使用示例
function rsaExample() {
const rsa = new RSA();
const message = 'Hello, RSA!';
const messageBigInt = RSA.stringToBigInt(message);
console.log('Original message:', message);
console.log('Message as BigInt:', messageBigInt.toString());
const encrypted = rsa.encrypt(messageBigInt);
console.log('Encrypted:', encrypted.toString());
const decrypted = rsa.decrypt(encrypted);
const decryptedMessage = RSA.bigIntToString(decrypted);
console.log('Decrypted message:', decryptedMessage);
console.log('Public Key:', rsa.getPublicKey());
console.log('Private Key:', rsa.getPrivateKey());
}2. AES 加密算法
ts
class AES {
private static readonly S_BOX = [
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16,
];
private static readonly RCON = [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36];
private key: Uint8Array;
private keySize: number;
private rounds: number;
private roundKeys: Uint8Array[];
constructor(key: Uint8Array) {
this.key = key;
this.keySize = key.length;
this.rounds = this.keySize === 16 ? 10 : this.keySize === 24 ? 12 : 14;
this.roundKeys = this.keyExpansion();
}
// 密钥扩展
private keyExpansion(): Uint8Array[] {
const roundKeys: Uint8Array[] = [];
const Nk = this.keySize / 4;
const Nr = this.rounds;
// 初始化
const w: number[] = [];
for (let i = 0; i < Nk; i++) {
w[i] = (this.key[4 * i] << 24) | (this.key[4 * i + 1] << 16) | (this.key[4 * i + 2] << 8) | this.key[4 * i + 3];
}
// 扩展密钥
for (let i = Nk; i < 4 * (Nr + 1); i++) {
let temp = w[i - 1];
if (i % Nk === 0) {
temp = this.subWord(this.rotWord(temp)) ^ AES.RCON[i / Nk];
} else if (Nk > 6 && i % Nk === 4) {
temp = this.subWord(temp);
}
w[i] = w[i - Nk] ^ temp;
}
// 转换为轮密钥
for (let i = 0; i <= Nr; i++) {
const roundKey = new Uint8Array(16);
for (let j = 0; j < 4; j++) {
const word = w[4 * i + j];
roundKey[4 * j] = (word >>> 24) & 0xff;
roundKey[4 * j + 1] = (word >>> 16) & 0xff;
roundKey[4 * j + 2] = (word >>> 8) & 0xff;
roundKey[4 * j + 3] = word & 0xff;
}
roundKeys.push(roundKey);
}
return roundKeys;
}
private subWord(word: number): number {
return (AES.S_BOX[(word >>> 24) & 0xff] << 24) | (AES.S_BOX[(word >>> 16) & 0xff] << 16) | (AES.S_BOX[(word >>> 8) & 0xff] << 8) | AES.S_BOX[word & 0xff];
}
private rotWord(word: number): number {
return ((word << 8) & 0xffffffff) | ((word >>> 24) & 0xff);
}
// 字节代换
private subBytes(state: Uint8Array): void {
for (let i = 0; i < 16; i++) {
state[i] = AES.S_BOX[state[i]];
}
}
// 行移位
private shiftRows(state: Uint8Array): void {
const temp = new Uint8Array(16);
// 第一行不变
temp[0] = state[0];
temp[4] = state[4];
temp[8] = state[8];
temp[12] = state[12];
// 第二行左移1位
temp[1] = state[5];
temp[5] = state[9];
temp[9] = state[13];
temp[13] = state[1];
// 第三行左移2位
temp[2] = state[10];
temp[6] = state[14];
temp[10] = state[2];
temp[14] = state[6];
// 第四行左移3位
temp[3] = state[15];
temp[7] = state[3];
temp[11] = state[7];
temp[15] = state[11];
state.set(temp);
}
// 列混淆
private mixColumns(state: Uint8Array): void {
for (let c = 0; c < 4; c++) {
const s0 = state[c];
const s1 = state[c + 4];
const s2 = state[c + 8];
const s3 = state[c + 12];
state[c] = this.gmul(0x02, s0) ^ this.gmul(0x03, s1) ^ s2 ^ s3;
state[c + 4] = s0 ^ this.gmul(0x02, s1) ^ this.gmul(0x03, s2) ^ s3;
state[c + 8] = s0 ^ s1 ^ this.gmul(0x02, s2) ^ this.gmul(0x03, s3);
state[c + 12] = this.gmul(0x03, s0) ^ s1 ^ s2 ^ this.gmul(0x02, s3);
}
}
private gmul(a: number, b: number): number {
let p = 0;
for (let i = 0; i < 8; i++) {
if (b & 1) p ^= a;
const hiBitSet = a & 0x80;
a <<= 1;
if (hiBitSet) a ^= 0x1b;
b >>= 1;
}
return p & 0xff;
}
// 轮密钥加
private addRoundKey(state: Uint8Array, roundKey: Uint8Array): void {
for (let i = 0; i < 16; i++) {
state[i] ^= roundKey[i];
}
}
// 加密单个块
private encryptBlock(block: Uint8Array): Uint8Array {
const state = new Uint8Array(block);
// 初始轮密钥加
this.addRoundKey(state, this.roundKeys[0]);
// 轮函数
for (let round = 1; round < this.rounds; round++) {
this.subBytes(state);
this.shiftRows(state);
this.mixColumns(state);
this.addRoundKey(state, this.roundKeys[round]);
}
// 最后一轮
this.subBytes(state);
this.shiftRows(state);
this.addRoundKey(state, this.roundKeys[this.rounds]);
return state;
}
// 解密单个块
private decryptBlock(block: Uint8Array): Uint8Array {
const state = new Uint8Array(block);
// 初始轮密钥加
this.addRoundKey(state, this.roundKeys[this.rounds]);
// 逆行移位和逆字节代换
for (let round = this.rounds - 1; round > 0; round--) {
this.invShiftRows(state);
this.invSubBytes(state);
this.addRoundKey(state, this.roundKeys[round]);
this.invMixColumns(state);
}
// 最后一轮
this.invShiftRows(state);
this.invSubBytes(state);
this.addRoundKey(state, this.roundKeys[0]);
return state;
}
// 逆字节代换
private invSubBytes(state: Uint8Array): void {
const INV_S_BOX = [
0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d,
];
for (let i = 0; i < 16; i++) {
state[i] = INV_S_BOX[state[i]];
}
}
// 逆行移位
private invShiftRows(state: Uint8Array): void {
const temp = new Uint8Array(16);
// 第一行不变
temp[0] = state[0];
temp[4] = state[4];
temp[8] = state[8];
temp[12] = state[12];
// 第二行右移1位
temp[1] = state[13];
temp[5] = state[1];
temp[9] = state[5];
temp[13] = state[9];
// 第三行右移2位
temp[2] = state[10];
temp[6] = state[14];
temp[10] = state[2];
temp[14] = state[6];
// 第四行右移3位
temp[3] = state[7];
temp[7] = state[11];
temp[11] = state[15];
temp[15] = state[3];
state.set(temp);
}
// 逆列混淆
private invMixColumns(state: Uint8Array): void {
for (let c = 0; c < 4; c++) {
const s0 = state[c];
const s1 = state[c + 4];
const s2 = state[c + 8];
const s3 = state[c + 12];
state[c] = this.gmul(0x0e, s0) ^ this.gmul(0x0b, s1) ^ this.gmul(0x0d, s2) ^ this.gmul(0x09, s3);
state[c + 4] = this.gmul(0x09, s0) ^ this.gmul(0x0e, s1) ^ this.gmul(0x0b, s2) ^ this.gmul(0x0d, s3);
state[c + 8] = this.gmul(0x0d, s0) ^ this.gmul(0x09, s1) ^ this.gmul(0x0e, s2) ^ this.gmul(0x0b, s3);
state[c + 12] = this.gmul(0x0b, s0) ^ this.gmul(0x0d, s1) ^ this.gmul(0x09, s2) ^ this.gmul(0x0e, s3);
}
}
// 加密 (ECB模式)
encrypt(data: Uint8Array): Uint8Array {
const blockSize = 16;
const paddedData = this.pad(data, blockSize);
const result = new Uint8Array(paddedData.length);
for (let i = 0; i < paddedData.length; i += blockSize) {
const block = paddedData.slice(i, i + blockSize);
const encryptedBlock = this.encryptBlock(block);
result.set(encryptedBlock, i);
}
return result;
}
// 解密 (ECB模式)
decrypt(data: Uint8Array): Uint8Array {
const blockSize = 16;
const result = new Uint8Array(data.length);
for (let i = 0; i < data.length; i += blockSize) {
const block = data.slice(i, i + blockSize);
const decryptedBlock = this.decryptBlock(block);
result.set(decryptedBlock, i);
}
return this.unpad(result);
}
// PKCS7填充
private pad(data: Uint8Array, blockSize: number): Uint8Array {
const padding = blockSize - (data.length % blockSize);
const padded = new Uint8Array(data.length + padding);
padded.set(data);
for (let i = data.length; i < padded.length; i++) {
padded[i] = padding;
}
return padded;
}
// 移除PKCS7填充
private unpad(data: Uint8Array): Uint8Array {
const padding = data[data.length - 1];
return data.slice(0, data.length - padding);
}
}
// 使用示例
function aesExample() {
const key = new Uint8Array(16); // 128位密钥
crypto.getRandomValues(key);
const aes = new AES(key);
const message = 'Hello, AES encryption!';
const data = new TextEncoder().encode(message);
console.log('Original:', message);
const encrypted = aes.encrypt(data);
console.log('Encrypted:', Array.from(encrypted));
const decrypted = aes.decrypt(encrypted);
const decryptedMessage = new TextDecoder().decode(decrypted);
console.log('Decrypted:', decryptedMessage);
}3. SHA-256 哈希算法
ts
class SHA256 {
private static readonly 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,
];
private h: number[] = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19];
// 右旋转
private rightRotate(value: number, amount: number): number {
return ((value >>> amount) | (value << (32 - amount))) >>> 0;
}
// SHA-256压缩函数
private compress(chunk: Uint8Array): void {
const w: number[] = new Array(64);
// 消息调度
for (let i = 0; i < 16; i++) {
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++) {
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;
}
// 初始化工作变量
let a = this.h[0],
b = this.h[1],
c = this.h[2],
d = this.h[3];
let e = this.h[4],
f = this.h[5],
g = this.h[6],
h = this.h[7];
// 主循环
for (let i = 0; i < 64; i++) {
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 + SHA256.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;
}
// 更新哈希值
this.h[0] = (this.h[0] + a) >>> 0;
this.h[1] = (this.h[1] + b) >>> 0;
this.h[2] = (this.h[2] + c) >>> 0;
this.h[3] = (this.h[3] + d) >>> 0;
this.h[4] = (this.h[4] + e) >>> 0;
this.h[5] = (this.h[5] + f) >>> 0;
this.h[6] = (this.h[6] + g) >>> 0;
this.h[7] = (this.h[7] + h) >>> 0;
}
// 计算哈希
hash(data: Uint8Array): string {
// 重置哈希状态
this.h = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19];
// 预处理:添加填充
const msgLen = data.length;
const paddedLen = Math.floor((msgLen + 8) / 64 + 1) * 64;
const padded = new Uint8Array(paddedLen);
padded.set(data);
padded[msgLen] = 0x80; // 添加 '1' 位
// 添加长度 (大端格式)
const lengthBits = BigInt(msgLen) * 8n;
for (let i = 0; i < 8; i++) {
padded[paddedLen - 8 + i] = Number((lengthBits >> BigInt(56 - i * 8)) & 0xffn);
}
// 处理每个512位块
for (let offset = 0; offset < paddedLen; offset += 64) {
this.compress(padded.slice(offset, offset + 64));
}
// 生成最终哈希
let result = '';
for (const h of this.h) {
result += (h >>> 0).toString(16).padStart(8, '0');
}
return result;
}
// 字符串哈希
hashString(str: string): string {
const encoder = new TextEncoder();
const data = encoder.encode(str);
return this.hash(data);
}
}
// 使用示例
function sha256Example() {
const sha256 = new SHA256();
const message = 'Hello, SHA-256!';
const hash = sha256.hashString(message);
console.log('Message:', message);
console.log('SHA-256:', hash);
console.log('Length:', hash.length * 4, 'bits');
}4. 实现要点
- RSA 使用大整数运算,基于数论原理。
- AES 是对称分组密码,使用 S-盒、移位、混淆等操作。
- SHA-256 是密码学哈希函数,产生256位哈希值。
- 所有实现都包含完整的加密/解密或哈希计算功能。
算法密码学JavaScript