SHA256
Миграция PDA на client.key
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import { base64ToBytes } from './crypto-utils.js';
|
||||
|
||||
function isByteArrayLike(value) {
|
||||
return Array.isArray(value) || ArrayBuffer.isView(value);
|
||||
}
|
||||
|
||||
function parseKeypairJson64(raw) {
|
||||
const text = String(raw || '').trim();
|
||||
if (!text.startsWith('[')) return null;
|
||||
|
||||
let parsed;
|
||||
try {
|
||||
parsed = JSON.parse(text);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!isByteArrayLike(parsed)) return null;
|
||||
const asArray = Array.from(parsed);
|
||||
if (asArray.length < 32) {
|
||||
throw new Error('Некорректный JSON ключ client.key: ожидалось минимум 32 байта');
|
||||
}
|
||||
|
||||
const out = new Uint8Array(asArray.length);
|
||||
for (let i = 0; i < asArray.length; i += 1) {
|
||||
const n = Number(asArray[i]);
|
||||
if (!Number.isInteger(n) || n < 0 || n > 255) {
|
||||
throw new Error('Некорректный JSON ключ client.key: найдены не-байтовые значения');
|
||||
}
|
||||
out[i] = n;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function extractSeed32FromPkcs8B64(pkcs8B64) {
|
||||
const bytes = base64ToBytes(String(pkcs8B64 || '').trim());
|
||||
if (bytes.length < 32) throw new Error('Некорректный PKCS8 ключ client.key');
|
||||
return bytes.slice(bytes.length - 32);
|
||||
}
|
||||
|
||||
export function extractClientKey32FromStoredValue(storedClientKey) {
|
||||
const raw = String(storedClientKey || '').trim();
|
||||
if (!raw) throw new Error('Пустой client.key');
|
||||
|
||||
const jsonBytes = parseKeypairJson64(raw);
|
||||
if (jsonBytes) {
|
||||
return jsonBytes.slice(0, 32);
|
||||
}
|
||||
|
||||
return extractSeed32FromPkcs8B64(raw);
|
||||
}
|
||||
Reference in New Issue
Block a user