@nice-code/util 0.6.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/index.js CHANGED
@@ -35,6 +35,14 @@ var createAesGcmKeyFromX25519Keys = async ({
35
35
  info
36
36
  }, ikm, { name: "AES-GCM", length: 256 }, false, ["encrypt", "decrypt"]);
37
37
  };
38
+ // src/crypto/aes_gcm/decryptBytesWithAesGcmKey.ts
39
+ var decryptBytesWithAesGcmKey = async ({
40
+ aesGcmKey,
41
+ dataToDecrypt
42
+ }) => {
43
+ const decryptedData = await crypto.subtle.decrypt({ name: "AES-GCM", iv: new Uint8Array(dataToDecrypt.nonce) }, aesGcmKey, new Uint8Array(dataToDecrypt.ciphertext));
44
+ return new Uint8Array(decryptedData);
45
+ };
38
46
  // src/crypto/aes_gcm/decryptTextDataWithAesGcmKey.ts
39
47
  import { base64 } from "@scure/base";
40
48
  var decryptTextDataWithAesGcmKey = async ({
@@ -47,6 +55,15 @@ var decryptTextDataWithAesGcmKey = async ({
47
55
  }, aesGcmKey, new Uint8Array(base64.decode(dataToDecrypt.ciphertext)));
48
56
  return new TextDecoder().decode(decryptedData);
49
57
  };
58
+ // src/crypto/aes_gcm/encryptBytesWithAesGcmKey.ts
59
+ var encryptBytesWithAesGcmKey = async ({
60
+ aesGcmKey,
61
+ dataToEncrypt
62
+ }) => {
63
+ const nonce = crypto.getRandomValues(new Uint8Array(12));
64
+ const encryptedData = await crypto.subtle.encrypt({ name: "AES-GCM", iv: nonce }, aesGcmKey, new Uint8Array(dataToEncrypt));
65
+ return { nonce, ciphertext: new Uint8Array(encryptedData) };
66
+ };
50
67
  // src/crypto/aes_gcm/encryptTextDataWithAesGcmKey.ts
51
68
  import { base64 as base642 } from "@scure/base";
52
69
  var encryptTextDataWithAesGcmKey = async ({
@@ -183,6 +200,16 @@ var vCreateSchema_TypeAndFormatPrefixedDataString = ({
183
200
  };
184
201
 
185
202
  // src/crypto/crypto.schema.ts
203
+ var ECryptoKeyAlgo;
204
+ ((ECryptoKeyAlgo2) => {
205
+ ECryptoKeyAlgo2["ed25519"] = "ed25519";
206
+ ECryptoKeyAlgo2["x25519"] = "x25519";
207
+ })(ECryptoKeyAlgo ||= {});
208
+ var ECryptoKeyFormat;
209
+ ((ECryptoKeyFormat2) => {
210
+ ECryptoKeyFormat2["raw_base64"] = "raw_base64";
211
+ ECryptoKeyFormat2["jwk"] = "jwk";
212
+ })(ECryptoKeyFormat ||= {});
186
213
  var vSerializedCryptoKeyDataEd25519_Raw = vCreateSchema_TypeAndFormatPrefixedDataString({
187
214
  format: "raw_base64" /* raw_base64 */,
188
215
  type: "ed25519" /* ed25519 */,
@@ -719,6 +746,26 @@ class ClientCryptoKeyLink {
719
746
  aesGcmKey: key
720
747
  });
721
748
  }
749
+ async encryptBytesForLinkedClient({
750
+ dataToEncrypt,
751
+ linkedClientId
752
+ }) {
753
+ const key = await this.getAesGcmKeyForLinkedClient(linkedClientId);
754
+ return await encryptBytesWithAesGcmKey({
755
+ dataToEncrypt,
756
+ aesGcmKey: key
757
+ });
758
+ }
759
+ async decryptBytesFromLinkedClient({
760
+ dataToDecrypt,
761
+ linkedClientId
762
+ }) {
763
+ const key = await this.getAesGcmKeyForLinkedClient(linkedClientId);
764
+ return await decryptBytesWithAesGcmKey({
765
+ dataToDecrypt,
766
+ aesGcmKey: key
767
+ });
768
+ }
722
769
  async signAndEncryptDataForLinkedClient({
723
770
  dataToEncrypt,
724
771
  linkedClientId
@@ -1011,6 +1058,15 @@ function createTypedMemoryStorage_json(options) {
1011
1058
  }
1012
1059
  export {
1013
1060
  verifyWithKeyEd25519,
1061
+ vVerifyChallengeWithSignature_WithThrow_Input,
1062
+ vVerifyChallengeWithSignature_Input,
1063
+ vSerializedCryptoKeyDataX25519_Raw,
1064
+ vSerializedCryptoKeyDataX25519_Jwk,
1065
+ vSerializedCryptoKeyDataEd25519_Raw,
1066
+ vSerializedCryptoKeyDataEd25519_Jwk,
1067
+ vEncryptedAesGcmPayload,
1068
+ vCryptoKeyPairDataX25519,
1069
+ vCryptoKeyPairDataEd25519,
1014
1070
  signTextDataWithKeyEd25519,
1015
1071
  signCombinedTextDataWithKeyEd25519,
1016
1072
  serializeX25519Key_Raw,
@@ -1022,7 +1078,9 @@ export {
1022
1078
  generateX25519KeyPair,
1023
1079
  generateEd25519KeyPair,
1024
1080
  encryptTextDataWithAesGcmKey,
1081
+ encryptBytesWithAesGcmKey,
1025
1082
  decryptTextDataWithAesGcmKey,
1083
+ decryptBytesWithAesGcmKey,
1026
1084
  createWebSessionStorageMethods,
1027
1085
  createWebSessionStorageAdapter,
1028
1086
  createWebLocalStorageMethods,
@@ -1056,6 +1114,8 @@ export {
1056
1114
  buildVerifyKeyBoundInfoString,
1057
1115
  StorageAdapter,
1058
1116
  EStorageAdapterType,
1117
+ ECryptoKeyFormat,
1118
+ ECryptoKeyAlgo,
1059
1119
  DEFAULT_COMBINED_TEXT_DATA_SEPARATOR,
1060
1120
  ClientCryptoKeyLink
1061
1121
  };
@@ -0,0 +1,9 @@
1
+ import type { TEncryptedAesGcmBytes } from "../crypto.schema";
2
+ /**
3
+ * Decrypts a raw-bytes AES-GCM payload (binary nonce + ciphertext) back to bytes. The counterpart of
4
+ * {@link decryptTextDataWithAesGcmKey}. AES-GCM verifies integrity, so a tampered ciphertext throws.
5
+ */
6
+ export declare const decryptBytesWithAesGcmKey: ({ aesGcmKey, dataToDecrypt, }: {
7
+ aesGcmKey: CryptoKey;
8
+ dataToDecrypt: TEncryptedAesGcmBytes;
9
+ }) => Promise<Uint8Array>;
@@ -0,0 +1,10 @@
1
+ import type { TEncryptedAesGcmBytes } from "../crypto.schema";
2
+ /**
3
+ * Encrypts raw bytes with an AES-GCM key, returning the binary nonce + ciphertext. The bytes
4
+ * counterpart of {@link encryptTextDataWithAesGcmKey} — use it for binary channels (msgpack frames)
5
+ * to avoid base64 inflation. A fresh 12-byte nonce is generated per call (never reuse a nonce).
6
+ */
7
+ export declare const encryptBytesWithAesGcmKey: ({ aesGcmKey, dataToEncrypt, }: {
8
+ aesGcmKey: CryptoKey;
9
+ dataToEncrypt: Uint8Array;
10
+ }) => Promise<TEncryptedAesGcmBytes>;
@@ -1,6 +1,6 @@
1
1
  import type { TTypeAndId } from "../../core/core_valibot_schemas";
2
2
  import type { StorageAdapter } from "../../storage_adapter/StorageAdapter";
3
- import type { TEncryptedAesGcmPayload, TSerializedCryptoKeyData_Ed25519_Raw, TSerializedCryptoKeyData_X25519_Raw } from "../crypto.schema";
3
+ import type { TEncryptedAesGcmBytes, TEncryptedAesGcmPayload, TSerializedCryptoKeyData_Ed25519_Raw, TSerializedCryptoKeyData_X25519_Raw } from "../crypto.schema";
4
4
  interface IClientCryptoKeyLink_Constructor {
5
5
  storageAdapter?: StorageAdapter;
6
6
  }
@@ -141,6 +141,20 @@ export declare class ClientCryptoKeyLink {
141
141
  private getAesGcmKeyForLinkedClient;
142
142
  encryptDataForLinkedClient({ dataToEncrypt, linkedClientId, }: IEncryptDataForLinkedClient): Promise<TEncryptedAesGcmPayload>;
143
143
  decryptDataFromLinkedClient({ dataToDecrypt, linkedClientId, }: IDecryptDataFromLinkedClient): Promise<string>;
144
+ /**
145
+ * Bytes counterpart of {@link encryptDataForLinkedClient} — encrypts raw bytes with the shared
146
+ * AES-GCM key, returning a binary nonce + ciphertext. Use it for binary channels (e.g. msgpack
147
+ * WebSocket frames) to avoid base64 inflation.
148
+ */
149
+ encryptBytesForLinkedClient({ dataToEncrypt, linkedClientId, }: {
150
+ dataToEncrypt: Uint8Array;
151
+ linkedClientId: TTypeAndId;
152
+ }): Promise<TEncryptedAesGcmBytes>;
153
+ /** Bytes counterpart of {@link decryptDataFromLinkedClient}. */
154
+ decryptBytesFromLinkedClient({ dataToDecrypt, linkedClientId, }: {
155
+ dataToDecrypt: TEncryptedAesGcmBytes;
156
+ linkedClientId: TTypeAndId;
157
+ }): Promise<Uint8Array>;
144
158
  signAndEncryptDataForLinkedClient({ dataToEncrypt, linkedClientId, }: IEncryptDataForLinkedClient): Promise<{
145
159
  encryptedData: TEncryptedAesGcmPayload;
146
160
  signatureBase64: string;
@@ -67,6 +67,15 @@ export declare const vEncryptedAesGcmPayload: v.ObjectSchema<{
67
67
  }, undefined>;
68
68
  export type TEncryptedAesGcmPayload = v.InferInput<typeof vEncryptedAesGcmPayload>;
69
69
  export type TEncryptedAesGcmPayload_Transformed = v.InferOutput<typeof vEncryptedAesGcmPayload>;
70
+ /**
71
+ * Raw-bytes counterpart of {@link TEncryptedAesGcmPayload} — keeps `nonce`/`ciphertext` as binary
72
+ * instead of base64 strings. For binary channels (e.g. msgpack WebSocket frames) this avoids the
73
+ * ~33% base64 inflation the text payload incurs.
74
+ */
75
+ export type TEncryptedAesGcmBytes = {
76
+ nonce: Uint8Array;
77
+ ciphertext: Uint8Array;
78
+ };
70
79
  interface ISerializedKeyData<T, P> {
71
80
  transformed: T;
72
81
  prefixed: P;
@@ -1,9 +1,12 @@
1
1
  export * from "./aes_gcm/createAesGcmKeyFromX25519Keys";
2
+ export * from "./aes_gcm/decryptBytesWithAesGcmKey";
2
3
  export * from "./aes_gcm/decryptTextDataWithAesGcmKey";
4
+ export * from "./aes_gcm/encryptBytesWithAesGcmKey";
3
5
  export * from "./aes_gcm/encryptTextDataWithAesGcmKey";
4
6
  export * from "./client_key_link/buildVerifyKeyBoundInfoString";
5
7
  export * from "./client_key_link/ClientCryptoKeyLink";
6
8
  export * from "./crypto.converters";
9
+ export * from "./crypto.schema";
7
10
  export * from "./ed25519/generateEd25519KeyPair";
8
11
  export * from "./ed25519/importEd25519Key";
9
12
  export * from "./ed25519/serializeEd25519Key_Jwk";
@@ -1,3 +1,4 @@
1
+ export type { TTypeAndId } from "./core/core_valibot_schemas";
1
2
  export * from "./crypto";
2
3
  export * from "./storage_adapter/StorageAdapter";
3
4
  export * from "./storage_adapter/specific/browser/browser_storage";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nice-code/util",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "exports": {