@julesl23/s5js
Version:
Enhanced TypeScript SDK for S5 decentralized storage with path-based API, media processing, and directory utilities
47 lines • 1.71 kB
JavaScript
import { BLAKE3, blake3 } from '@noble/hashes/blake3';
import { KeyPairEd25519 } from "../crypto.js";
import { xchacha20poly1305 } from '@noble/ciphers/chacha';
import * as ed from '@noble/ed25519';
export class JSCryptoImplementation {
generateSecureRandomBytes(length) {
const array = new Uint8Array(length);
crypto.getRandomValues(array);
return array;
}
async hashBlake3(input) {
return blake3(input);
}
hashBlake3Sync(input) {
return blake3(input);
}
async hashBlake3Blob(blob) {
const blake3Hasher = new BLAKE3();
// TODO Adjust chunk size
const chunkSize = 256 * 1024;
for (let i = 0; i < blob.size; i += chunkSize) {
const chunk = blob.slice(i, i + chunkSize);
blake3Hasher.update(new Uint8Array(await chunk.arrayBuffer()));
}
return blake3Hasher.digest();
}
// TODO(perf): use ed25519 web APIs if available
async verifyEd25519(publicKey, message, signature) {
return await ed.verifyAsync(signature, message, publicKey);
}
async signEd25519(keyPair, message) {
return await ed.signAsync(message, keyPair.privKey);
}
async newKeyPairEd25519(seed) {
const pubKey = await ed.getPublicKeyAsync(seed);
return new KeyPairEd25519(seed, pubKey);
}
async encryptXChaCha20Poly1305(key, nonce, plaintext) {
const chacha = xchacha20poly1305(key, nonce);
return chacha.encrypt(plaintext);
}
async decryptXChaCha20Poly1305(key, nonce, ciphertext) {
const chacha = xchacha20poly1305(key, nonce);
return chacha.decrypt(ciphertext);
}
}
//# sourceMappingURL=js.js.map