mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-07-14 03:45:41 +00:00
Initial rebrand: @polkadot -> @pezkuwi (14 packages)
- Package namespace: @polkadot/* -> @pezkuwi/* - Repository: polkadot-js/common -> pezkuwichain/pezkuwi-common - Author: Pezkuwi Team <team@pezkuwichain.io> Core packages: - @pezkuwi/util (utilities) - @pezkuwi/util-crypto (crypto primitives) - @pezkuwi/keyring (account management) - @pezkuwi/networks (chain metadata) - @pezkuwi/hw-ledger (Ledger hardware wallet) - @pezkuwi/x-* (10 polyfill packages) Total: 14 packages Upstream: polkadot-js/common v14.0.1
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
// Copyright 2017-2025 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { naclDecrypt, naclEncrypt } from './index.js';
|
||||
|
||||
describe('naclDecrypt', (): void => {
|
||||
it('decrypts a encrypted message', (): void => {
|
||||
const secret = new Uint8Array(32);
|
||||
const message = new Uint8Array([1, 2, 3, 4, 5, 4, 3, 2, 1]);
|
||||
const { encrypted, nonce } = naclEncrypt(message, secret);
|
||||
|
||||
expect(
|
||||
naclDecrypt(encrypted, nonce, secret)
|
||||
).toEqual(
|
||||
message
|
||||
);
|
||||
});
|
||||
|
||||
it('returns null on invalid', (): void => {
|
||||
expect(
|
||||
naclDecrypt(new Uint8Array(), new Uint8Array(24), new Uint8Array(32))
|
||||
).toEqual(null);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
// Copyright 2017-2025 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { naclSecretboxOpen } from './tweetnacl.js';
|
||||
|
||||
/**
|
||||
* @name naclDecrypt
|
||||
* @summary Decrypts a message using the supplied secretKey and nonce
|
||||
* @description
|
||||
* Returns an decrypted message, using the `secret` and `nonce`.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { naclDecrypt } from '@pezkuwi/util-crypto';
|
||||
*
|
||||
* naclDecrypt([...], [...], [...]); // => [...]
|
||||
* ```
|
||||
*/
|
||||
export function naclDecrypt (encrypted: Uint8Array, nonce: Uint8Array, secret: Uint8Array): Uint8Array | null {
|
||||
return naclSecretboxOpen(encrypted, nonce, secret);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright 2017-2025 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { naclEncrypt } from './index.js';
|
||||
|
||||
describe('naclEncrypt', (): void => {
|
||||
it('encrypts a message', (): void => {
|
||||
const secret = new Uint8Array(32);
|
||||
const message = new Uint8Array([1, 2, 3, 4, 5, 4, 3, 2, 1]);
|
||||
|
||||
expect(
|
||||
naclEncrypt(message, secret, new Uint8Array(24))
|
||||
).toEqual({
|
||||
encrypted: new Uint8Array([94, 21, 20, 69, 68, 221, 140, 245, 200, 67, 77, 188, 129, 85, 227, 141, 199, 60, 184, 251, 251, 129, 205, 46, 234]),
|
||||
nonce: new Uint8Array(24)
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
// Copyright 2017-2025 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { randomAsU8a } from '../random/asU8a.js';
|
||||
import { naclSecretbox } from './tweetnacl.js';
|
||||
|
||||
interface Encrypted {
|
||||
encrypted: Uint8Array;
|
||||
nonce: Uint8Array;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name naclEncrypt
|
||||
* @summary Encrypts a message using the supplied secretKey and nonce
|
||||
* @description
|
||||
* Returns an encrypted message, using the `secretKey` and `nonce`. If the `nonce` was not supplied, a random value is generated.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { naclEncrypt } from '@pezkuwi/util-crypto';
|
||||
*
|
||||
* naclEncrypt([...], [...]); // => [...]
|
||||
* ```
|
||||
*/
|
||||
export function naclEncrypt (message: Uint8Array, secret: Uint8Array, nonce: Uint8Array = randomAsU8a(24)): Encrypted {
|
||||
return {
|
||||
encrypted: naclSecretbox(message, nonce, secret),
|
||||
nonce
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// Copyright 2017-2025 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @summary Implements [NaCl](http://nacl.cr.yp.to/) secret-key authenticated encryption, public-key authenticated encryption
|
||||
*/
|
||||
export { naclDecrypt } from './decrypt.js';
|
||||
export { naclEncrypt } from './encrypt.js';
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,161 @@
|
||||
// Copyright 2017-2025 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
// Adapted from https://github.com/dchest/tweetnacl-js/blob/6a9594a35a27f9c723c5f1c107e376d1c65c23b3/test/04-secretbox.quick.js
|
||||
// Adapted from https://github.com/dchest/tweetnacl-js/blob/6a9594a35a27f9c723c5f1c107e376d1c65c23b3/test/04-secretbox.js
|
||||
//
|
||||
// Changes made:
|
||||
// - Jest-like test environment (not tape)
|
||||
// - Combine "quick" and "random" tests into single file
|
||||
//
|
||||
// Original headers:
|
||||
//
|
||||
// Ported in 2014 by Dmitry Chestnykh and Devi Mandiri.
|
||||
// Public domain.
|
||||
//
|
||||
// Implementation derived from TweetNaCl version 20140427.
|
||||
// See for details: http://tweetnacl.cr.yp.to/
|
||||
|
||||
import { stringToU8a, u8aEq } from '@pezkuwi/util';
|
||||
|
||||
import { base64Decode } from '../base64/index.js';
|
||||
import { naclSecretbox, naclSecretboxOpen } from './tweetnacl.js';
|
||||
import { TEST_DATA } from './tweetnacl-secretbox-data.spec.js';
|
||||
|
||||
describe('tweetnacl/secretbox', (): void => {
|
||||
describe('tweetnacl/test/04-secretbox.quick.js', (): void => {
|
||||
it('naclSecretbox and naclSecretboxOpen', (): void => {
|
||||
const key = new Uint8Array(32);
|
||||
const nonce = new Uint8Array(24);
|
||||
|
||||
for (let i = 0; i < key.length; i++) {
|
||||
key[i] = i & 0xff;
|
||||
}
|
||||
|
||||
for (let i = 0; i < nonce.length; i++) {
|
||||
nonce[i] = (32 + i) & 0xff;
|
||||
}
|
||||
|
||||
const msg = stringToU8a('message to encrypt');
|
||||
const box = naclSecretbox(msg, nonce, key);
|
||||
const openedMsg = naclSecretboxOpen(box, nonce, key);
|
||||
|
||||
expect(
|
||||
!!openedMsg && u8aEq(openedMsg, msg)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('naclSecretbox.open with invalid box', (): void => {
|
||||
const key = new Uint8Array(32);
|
||||
const nonce = new Uint8Array(24);
|
||||
|
||||
expect(
|
||||
naclSecretboxOpen(new Uint8Array(0), nonce, key)
|
||||
).toBe(null);
|
||||
|
||||
expect(
|
||||
naclSecretboxOpen(new Uint8Array(10), nonce, key)
|
||||
).toBe(null);
|
||||
|
||||
expect(
|
||||
naclSecretboxOpen(new Uint8Array(100), nonce, key)
|
||||
).toBe(null);
|
||||
});
|
||||
|
||||
it('naclSecretbox.open with invalid nonce', (): void => {
|
||||
const key = new Uint8Array(32);
|
||||
const nonce = new Uint8Array(24);
|
||||
|
||||
for (let i = 0; i < nonce.length; i++) {
|
||||
nonce[i] = i & 0xff;
|
||||
}
|
||||
|
||||
const msg = stringToU8a('message to encrypt');
|
||||
const box = naclSecretbox(msg, nonce, key);
|
||||
const unbox = naclSecretboxOpen(box, nonce, key);
|
||||
|
||||
expect(
|
||||
!!unbox && u8aEq(unbox, msg)
|
||||
).toBe(true);
|
||||
|
||||
nonce[0] = 255;
|
||||
|
||||
expect(
|
||||
naclSecretboxOpen(box, nonce, key)
|
||||
).toBe(null);
|
||||
});
|
||||
|
||||
it('naclSecretbox.open with invalid key', (): void => {
|
||||
const key = new Uint8Array(32);
|
||||
|
||||
for (let i = 0; i < key.length; i++) {
|
||||
key[i] = i & 0xff;
|
||||
}
|
||||
|
||||
const nonce = new Uint8Array(24);
|
||||
const msg = stringToU8a('message to encrypt');
|
||||
const box = naclSecretbox(msg, nonce, key);
|
||||
const unbox = naclSecretboxOpen(box, nonce, key);
|
||||
|
||||
expect(
|
||||
!!unbox && u8aEq(unbox, msg)
|
||||
).toBe(true);
|
||||
|
||||
key[0] = 255;
|
||||
|
||||
expect(
|
||||
naclSecretboxOpen(box, nonce, key)
|
||||
).toBe(null);
|
||||
});
|
||||
|
||||
it('naclSecretbox with message lengths of 0 to 1024', (): void => {
|
||||
const key = new Uint8Array(32);
|
||||
const nonce = new Uint8Array(24);
|
||||
const fullMsg = new Uint8Array(1024);
|
||||
|
||||
for (let i = 0; i < key.length; i++) {
|
||||
key[i] = i & 0xff;
|
||||
}
|
||||
|
||||
for (let i = 0; i < fullMsg.length; i++) {
|
||||
fullMsg[i] = i & 0xff;
|
||||
}
|
||||
|
||||
for (let i = 0; i < fullMsg.length; i++) {
|
||||
const msg = fullMsg.subarray(0, i);
|
||||
const box = naclSecretbox(msg, nonce, key);
|
||||
const unbox = naclSecretboxOpen(box, nonce, key);
|
||||
|
||||
expect(
|
||||
!!unbox && u8aEq(unbox, msg)
|
||||
).toBe(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('tweetnacl/test/04-secretbox.js', (): void => {
|
||||
for (let i = 0, count = TEST_DATA.length; i < count; i++) {
|
||||
const [keyBase64, nonceBase64, msgBase64, expBase64] = TEST_DATA[i];
|
||||
|
||||
it(`is ok on "${msgBase64}"`, (): void => {
|
||||
const key = base64Decode(keyBase64);
|
||||
const nonce = base64Decode(nonceBase64);
|
||||
const msg = base64Decode(msgBase64);
|
||||
const goodBox = base64Decode(expBase64);
|
||||
const box = naclSecretbox(msg, nonce, key);
|
||||
|
||||
expect(
|
||||
u8aEq(box, goodBox)
|
||||
).toBe(true);
|
||||
|
||||
const unbox = naclSecretboxOpen(goodBox, nonce, key);
|
||||
|
||||
expect(
|
||||
!!unbox && u8aEq(unbox, msg)
|
||||
).toBe(true);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user