mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-04-22 09:08:03 +00:00
chore: update to version 14.0.11 and align website URLs
This commit is contained in:
+6
@@ -0,0 +1,6 @@
|
||||
import type { EncryptedJsonEncoding, EncryptedJsonVersion } from './types.js';
|
||||
export declare const ENCODING: EncryptedJsonEncoding[];
|
||||
export declare const ENCODING_NONE: EncryptedJsonEncoding[];
|
||||
export declare const ENCODING_VERSION: EncryptedJsonVersion;
|
||||
export declare const NONCE_LENGTH = 24;
|
||||
export declare const SCRYPT_LENGTH: number;
|
||||
@@ -0,0 +1,5 @@
|
||||
export const ENCODING = ['scrypt', 'xsalsa20-poly1305'];
|
||||
export const ENCODING_NONE = ['none'];
|
||||
export const ENCODING_VERSION = '3';
|
||||
export const NONCE_LENGTH = 24;
|
||||
export const SCRYPT_LENGTH = 32 + (3 * 4);
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
import type { EncryptedJson } from './types.js';
|
||||
export declare function jsonDecrypt({ encoded, encoding }: EncryptedJson, passphrase?: string | null): Uint8Array;
|
||||
@@ -0,0 +1,13 @@
|
||||
import { hexToU8a, isHex } from '@pezkuwi/util';
|
||||
import { base64Decode } from '../base64/index.js';
|
||||
import { jsonDecryptData } from './decryptData.js';
|
||||
export function jsonDecrypt({ encoded, encoding }, passphrase) {
|
||||
if (!encoded) {
|
||||
throw new Error('No encrypted data available to decode');
|
||||
}
|
||||
return jsonDecryptData(isHex(encoded)
|
||||
? hexToU8a(encoded)
|
||||
: base64Decode(encoded), passphrase, Array.isArray(encoding.type)
|
||||
? encoding.type
|
||||
: [encoding.type]);
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
import type { EncryptedJsonEncoding } from './types.js';
|
||||
export declare function jsonDecryptData(encrypted?: Uint8Array | null, passphrase?: string | null, encType?: EncryptedJsonEncoding[]): Uint8Array;
|
||||
@@ -0,0 +1,29 @@
|
||||
import { stringToU8a, u8aFixLength } from '@pezkuwi/util';
|
||||
import { naclDecrypt } from '../nacl/index.js';
|
||||
import { scryptEncode, scryptFromU8a } from '../scrypt/index.js';
|
||||
import { ENCODING, NONCE_LENGTH, SCRYPT_LENGTH } from './constants.js';
|
||||
export function jsonDecryptData(encrypted, passphrase, encType = ENCODING) {
|
||||
if (!encrypted) {
|
||||
throw new Error('No encrypted data available to decode');
|
||||
}
|
||||
else if (encType.includes('xsalsa20-poly1305') && !passphrase) {
|
||||
throw new Error('Password required to decode encrypted data');
|
||||
}
|
||||
let encoded = encrypted;
|
||||
if (passphrase) {
|
||||
let password;
|
||||
if (encType.includes('scrypt')) {
|
||||
const { params, salt } = scryptFromU8a(encrypted);
|
||||
password = scryptEncode(passphrase, salt, params).password;
|
||||
encrypted = encrypted.subarray(SCRYPT_LENGTH);
|
||||
}
|
||||
else {
|
||||
password = stringToU8a(passphrase);
|
||||
}
|
||||
encoded = naclDecrypt(encrypted.subarray(NONCE_LENGTH), encrypted.subarray(0, NONCE_LENGTH), u8aFixLength(password, 256, true));
|
||||
}
|
||||
if (!encoded) {
|
||||
throw new Error('Unable to decode using the supplied passphrase');
|
||||
}
|
||||
return encoded;
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
import type { EncryptedJson } from './types.js';
|
||||
export declare function jsonEncrypt(data: Uint8Array, contentType: string[], passphrase?: string | null): EncryptedJson;
|
||||
@@ -0,0 +1,15 @@
|
||||
import { u8aConcat } from '@pezkuwi/util';
|
||||
import { naclEncrypt } from '../nacl/index.js';
|
||||
import { scryptEncode, scryptToU8a } from '../scrypt/index.js';
|
||||
import { jsonEncryptFormat } from './encryptFormat.js';
|
||||
export function jsonEncrypt(data, contentType, passphrase) {
|
||||
let isEncrypted = false;
|
||||
let encoded = data;
|
||||
if (passphrase) {
|
||||
const { params, password, salt } = scryptEncode(passphrase);
|
||||
const { encrypted, nonce } = naclEncrypt(encoded, password.subarray(0, 32));
|
||||
isEncrypted = true;
|
||||
encoded = u8aConcat(scryptToU8a(salt, params), nonce, encrypted);
|
||||
}
|
||||
return jsonEncryptFormat(encoded, contentType, isEncrypted);
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
import type { EncryptedJson } from './types.js';
|
||||
export declare function jsonEncryptFormat(encoded: Uint8Array, contentType: string[], isEncrypted: boolean): EncryptedJson;
|
||||
@@ -0,0 +1,14 @@
|
||||
import { base64Encode } from '../base64/index.js';
|
||||
import { ENCODING, ENCODING_NONE, ENCODING_VERSION } from './constants.js';
|
||||
export function jsonEncryptFormat(encoded, contentType, isEncrypted) {
|
||||
return {
|
||||
encoded: base64Encode(encoded),
|
||||
encoding: {
|
||||
content: contentType,
|
||||
type: isEncrypted
|
||||
? ENCODING
|
||||
: ENCODING_NONE,
|
||||
version: ENCODING_VERSION
|
||||
}
|
||||
};
|
||||
}
|
||||
Vendored
+4
@@ -0,0 +1,4 @@
|
||||
export { jsonDecrypt } from './decrypt.js';
|
||||
export { jsonDecryptData } from './decryptData.js';
|
||||
export { jsonEncrypt } from './encrypt.js';
|
||||
export { jsonEncryptFormat } from './encryptFormat.js';
|
||||
@@ -0,0 +1,4 @@
|
||||
export { jsonDecrypt } from './decrypt.js';
|
||||
export { jsonDecryptData } from './decryptData.js';
|
||||
export { jsonEncrypt } from './encrypt.js';
|
||||
export { jsonEncryptFormat } from './encryptFormat.js';
|
||||
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
export type EncryptedJsonVersion = '0' | '1' | '2' | '3';
|
||||
export type EncryptedJsonEncoding = 'none' | 'scrypt' | 'xsalsa20-poly1305';
|
||||
export interface EncryptedJsonDescriptor {
|
||||
/** Descriptor for the content */
|
||||
content: string[];
|
||||
/** The encoding (in current/latest versions this is always an array) */
|
||||
type: EncryptedJsonEncoding | EncryptedJsonEncoding[];
|
||||
/** The version of encoding applied */
|
||||
version: EncryptedJsonVersion;
|
||||
}
|
||||
export interface EncryptedJson {
|
||||
/** The encoded string */
|
||||
encoded: string;
|
||||
/** The encoding used */
|
||||
encoding: EncryptedJsonDescriptor;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export {};
|
||||
Reference in New Issue
Block a user