区块链开发:从哈希生成到冲突解决
1. 区块链哈希生成
在区块链开发中,哈希生成是关键环节,它用于确保数据的完整性和安全性。为了让区块链能同时适用于Web和独立应用,我们需要使用两种不同的加密API来生成SHA - 256哈希:
- Web应用 :可以调用所有浏览器支持的 crypto 对象的API。
- 独立应用 :在Node.js运行时环境下,使用Node.js自带的 crypto 模块(https://nodejs.org/api/crypto.html)。
为了让客户端应用在运行时自动选择合适的API,我们创建了一个库。以下是 lib/universal_sha256.ts 文件中的代码:
function sha256_node(data: string): Promise<string> {
const crypto = require('crypto');
return Promise.resolve(crypto.createHash('sha256').update(data).digest('hex'));
}
async function sha256_browser(data: string): Promise<string> {
const msgUint8Array = new TextEncoder().encode(data);
const
超级会员免费看
订阅专栏 解锁全文
19

被折叠的 条评论
为什么被折叠?



