/
raiden
/
obsidian-git-encrypt
Обзор
Документация
Войти
/
raiden
/
obsidian-git-encrypt
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/crypto/encryptedEnvelope.ts
47 строк
1 KB
Robert Kuzhin
feat: implement encrypted vault crypto core
09 авг 2026, 09:55
09 авг 2026, 09:55
54bad50
Код
Авторство
О чём код?
import { AEAD_TAG_BYTES, FORMAT_VERSION, NONCE_BYTES } from "./constants.js"; import { EnvelopeError } from "./errors.js"; const MAGIC = new Uint8Array([0x4f, 0x45, 0x47, 0x43]); // OEGC const HEADER_BYTES = MAGIC.length + 1 + NONCE_BYTES; export interface DecodedEnvelope { readonly nonce: Uint8Array; readonly ciphertext: Uint8Array; } export function encodeEnvelope(nonce: Uint8Array, ciphertext: Uint8Array): Uint8Array { if (nonce.length !== NONCE_BYTES) { throw new EnvelopeError(`Nonce must contain ${NONCE_BYTES} bytes`); } if (ciphertext.length < AEAD_TAG_BYTES) { throw new EnvelopeError("Ciphertext is shorter than its authentication tag"); } const result = new Uint8Array(HEADER_BYTES + ciphertext.length); result.set(MAGIC, 0); result[MAGIC.length] = FORMAT_VERSION; result.set(nonce, MAGIC.length + 1); result.set(ciphertext, HEADER_BYTES); return result; } export function decodeEnvelope(data: Uint8Array): DecodedEnvelope { if (data.length < HEADER_BYTES + AEAD_TAG_BYTES) { throw new EnvelopeError("Encrypted envelope is truncated"); } for (let index = 0; index < MAGIC.length; index += 1) { if (data[index] !== MAGIC[index]) { throw new EnvelopeError("Encrypted envelope has invalid magic bytes"); } } if (data[MAGIC.length] !== FORMAT_VERSION) { throw new EnvelopeError("Encrypted envelope version is not supported"); } return { nonce: data.slice(MAGIC.length + 1, HEADER_BYTES), ciphertext: data.slice(HEADER_BYTES), }; }