UNPKG

eths-git

Version:

**eths-git-remote** is a decentralized Git solution designed to manage repositories on-chain. It provides two main components:

34 lines (33 loc) 1.36 kB
import { randomBytes, pbkdf2Sync, createCipheriv, createDecipheriv } from 'crypto'; export function encrypt(data, password) { const salt = randomBytes(16); const key = pbkdf2Sync(password, salt, 100000, 32, 'sha256'); const iv = randomBytes(12); const cipher = createCipheriv('aes-256-gcm', key, iv, { authTagLength: 16 }); const encrypted = Buffer.concat([cipher.update(data, 'utf8'), cipher.final()]); const authTag = cipher.getAuthTag(); return [ salt.toString('hex'), iv.toString('hex'), authTag.toString('hex'), encrypted.toString('hex') ].join(':'); } export function decrypt(encryptedStr, key) { const [saltHex, ivHex, authTagHex, encryptedHex] = encryptedStr.split(':'); const iv = Buffer.from(ivHex, 'hex'); const authTag = Buffer.from(authTagHex, 'hex'); const encrypted = Buffer.from(encryptedHex, 'hex'); const decipher = createDecipheriv('aes-256-gcm', key, iv, { authTagLength: 16 }); decipher.setAuthTag(authTag); const decrypted = Buffer.concat([ decipher.update(encrypted), decipher.final() ]); return decrypted.toString('utf8'); } export function deriveKey(password, saltHex) { const salt = Buffer.from(saltHex, 'hex'); const key = pbkdf2Sync(password, salt, 100000, 32, 'sha256'); return key.toString('hex'); }