SHA256
НЕ ПРОВЕРЕНО: DM-вложения, upload файлов и ревизии личных сообщений
This commit is contained in:
@@ -25,6 +25,10 @@ function base64UrlToBase64(value) {
|
||||
return normalized + '='.repeat(padLen);
|
||||
}
|
||||
|
||||
function base64ToBase64Url(value) {
|
||||
return String(value || '').replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/g, '');
|
||||
}
|
||||
|
||||
const BASE58_ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
|
||||
|
||||
export function bytesToBase58(bytes) {
|
||||
@@ -93,6 +97,10 @@ export function bytesToBase64(bytes) {
|
||||
return btoa(binary);
|
||||
}
|
||||
|
||||
export function bytesToBase64Url(bytes) {
|
||||
return base64ToBase64Url(bytesToBase64(bytes));
|
||||
}
|
||||
|
||||
export function base64ToBytes(base64) {
|
||||
const normalized = (base64 || '').trim();
|
||||
const binary = atob(normalized);
|
||||
@@ -103,6 +111,10 @@ export function base64ToBytes(base64) {
|
||||
return bytes;
|
||||
}
|
||||
|
||||
export function base64UrlToBytes(value) {
|
||||
return base64ToBytes(base64UrlToBase64(String(value || '').trim()));
|
||||
}
|
||||
|
||||
export function utf8Bytes(value) {
|
||||
return encoder.encode(value);
|
||||
}
|
||||
@@ -260,6 +272,28 @@ export async function decryptJsonWithStoragePwd(envelope, storagePwd) {
|
||||
return JSON.parse(text);
|
||||
}
|
||||
|
||||
export async function importAesKeyRaw(keyBytes, usages = ['encrypt', 'decrypt']) {
|
||||
return getSubtleApi().importKey('raw', keyBytes, { name: 'AES-GCM' }, false, usages);
|
||||
}
|
||||
|
||||
export async function encryptBytesAesGcm(plainBytes, keyBytes, ivBytes) {
|
||||
const key = await importAesKeyRaw(keyBytes, ['encrypt']);
|
||||
const cipher = await getSubtleApi().encrypt({ name: 'AES-GCM', iv: ivBytes }, key, plainBytes);
|
||||
return new Uint8Array(cipher);
|
||||
}
|
||||
|
||||
export async function decryptBytesAesGcm(cipherBytes, keyBytes, ivBytes) {
|
||||
const key = await importAesKeyRaw(keyBytes, ['decrypt']);
|
||||
const plain = await getSubtleApi().decrypt({ name: 'AES-GCM', iv: ivBytes }, key, cipherBytes);
|
||||
return new Uint8Array(plain);
|
||||
}
|
||||
|
||||
export function randomBytes(byteLen = 32) {
|
||||
const out = new Uint8Array(byteLen);
|
||||
getCryptoApi().getRandomValues(out);
|
||||
return out;
|
||||
}
|
||||
|
||||
export async function generateEd25519Pair() {
|
||||
return getSubtleApi().generateKey({ name: 'Ed25519' }, true, ['sign', 'verify']);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user