@push.rocks/smartcrypto 2.0.4 → 2.1.0

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.
@@ -0,0 +1,62 @@
1
+ {
2
+ "@git.zone/cli": {
3
+ "schemaVersion": 2,
4
+ "projectType": "npm",
5
+ "module": {
6
+ "githost": "code.foss.global",
7
+ "gitscope": "push.rocks",
8
+ "gitrepo": "smartcrypto",
9
+ "description": "A library providing easy methods for cryptographic operations, including key pair generation.",
10
+ "npmPackagename": "@push.rocks/smartcrypto",
11
+ "license": "MIT",
12
+ "projectDomain": "push.rocks",
13
+ "keywords": [
14
+ "cryptography",
15
+ "encryption",
16
+ "security",
17
+ "public key",
18
+ "private key",
19
+ "RSA",
20
+ "key pair generation",
21
+ "node-forge",
22
+ "typescript"
23
+ ]
24
+ },
25
+ "release": {
26
+ "confirmation": "prompt",
27
+ "preflight": {
28
+ "requireCleanTree": true,
29
+ "test": false,
30
+ "build": true
31
+ },
32
+ "targets": {
33
+ "git": {
34
+ "enabled": true,
35
+ "remote": "origin",
36
+ "pushBranch": true,
37
+ "pushTags": true
38
+ },
39
+ "npm": {
40
+ "enabled": true,
41
+ "registries": [
42
+ "https://verdaccio.lossless.digital",
43
+ "https://registry.npmjs.org"
44
+ ],
45
+ "accessLevel": "public",
46
+ "alreadyPublished": "success"
47
+ },
48
+ "docker": {
49
+ "enabled": false,
50
+ "engine": "tsdocker",
51
+ "patterns": []
52
+ }
53
+ }
54
+ }
55
+ },
56
+ "@git.zone/tsdoc": {
57
+ "legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
58
+ },
59
+ "@ship.zone/szci": {
60
+ "npmGlobalTools": []
61
+ }
62
+ }
@@ -1,5 +1,5 @@
1
1
  /**
2
- * autocreated commitinfo by @pushrocks/commitinfo
2
+ * autocreated commitinfo by @push.rocks/commitinfo
3
3
  */
4
4
  export declare const commitinfo: {
5
5
  name: string;
@@ -1,9 +1,9 @@
1
1
  /**
2
- * autocreated commitinfo by @pushrocks/commitinfo
2
+ * autocreated commitinfo by @push.rocks/commitinfo
3
3
  */
4
4
  export const commitinfo = {
5
5
  name: '@push.rocks/smartcrypto',
6
- version: '2.0.4',
7
- description: 'easy crypto methods'
6
+ version: '2.1.0',
7
+ description: 'A library providing easy methods for cryptographic operations, including key pair generation.'
8
8
  };
9
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiMDBfY29tbWl0aW5mb19kYXRhLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvMDBfY29tbWl0aW5mb19kYXRhLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOztHQUVHO0FBQ0gsTUFBTSxDQUFDLE1BQU0sVUFBVSxHQUFHO0lBQ3hCLElBQUksRUFBRSx5QkFBeUI7SUFDL0IsT0FBTyxFQUFFLE9BQU87SUFDaEIsV0FBVyxFQUFFLHFCQUFxQjtDQUNuQyxDQUFBIn0=
9
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiMDBfY29tbWl0aW5mb19kYXRhLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvMDBfY29tbWl0aW5mb19kYXRhLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOztHQUVHO0FBQ0gsTUFBTSxDQUFDLE1BQU0sVUFBVSxHQUFHO0lBQ3hCLElBQUksRUFBRSx5QkFBeUI7SUFDL0IsT0FBTyxFQUFFLE9BQU87SUFDaEIsV0FBVyxFQUFFLCtGQUErRjtDQUM3RyxDQUFBIn0=
@@ -1,10 +1,9 @@
1
- /// <reference types="node-forge" />
2
1
  import * as plugins from './smartcrypto.plugins.js';
3
2
  export declare class PrivateKey {
4
3
  static createFromPrivateKey(pemString: string): PrivateKey;
5
4
  static fromPemString(pemString: string): PrivateKey;
6
- forgePrivateKey: plugins.nodeForge.pki.PrivateKey;
7
- constructor(privateKeyArg: plugins.nodeForge.pki.PrivateKey);
5
+ forgePrivateKey: plugins.nodeForge.pki.rsa.PrivateKey;
6
+ constructor(privateKeyArg: plugins.nodeForge.pki.rsa.PrivateKey);
8
7
  toPemString(): string;
9
8
  toOpenSSH(): string;
10
9
  }
@@ -18,4 +18,4 @@ export class PrivateKey {
18
18
  return plugins.nodeForge.ssh.privateKeyToOpenSSH(this.forgePrivateKey, 'user@host');
19
19
  }
20
20
  }
21
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRjcnlwdG8uY2xhc3Nlcy5wcml2YXRla2V5LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRjcnlwdG8uY2xhc3Nlcy5wcml2YXRla2V5LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sS0FBSyxPQUFPLE1BQU0sMEJBQTBCLENBQUM7QUFFcEQsTUFBTSxPQUFPLFVBQVU7SUFDckIsU0FBUztJQUNGLE1BQU0sQ0FBQyxvQkFBb0IsQ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1BQU0sQ0FBQyxvQkFBb0IsQ0FBQyxTQUFpQjtRQUNsRCxNQUFNLFVBQVUsR0FBRyxPQUFPLENBQUMsU0FBUyxDQUFDLEdBQUcsQ0FBQyxpQkFBaUIsQ0FBQyxTQUFTLENBQUMsQ0FBQztRQUN0RSxPQUFPLElBQUksVUFBVSxDQUFDLFVBQVUsQ0FBQyxDQUFDO0lBQ3BDLENBQUM7SUFFTSxNQUFNLENBQUMsYUFBYSxDQUFDLFNBQWlCO1FBQzNDLE9BQU8sSUFBSSxVQUFVLENBQUMsT0FBTyxDQUFDLFNBQVMsQ0FBQyxHQUFHLENBQUMsaUJBQWlCLENBQUMsU0FBUyxDQUFDLENBQUMsQ0FBQztJQUM1RSxDQUFDO0lBS0QsWUFBWSxhQUFtRDtRQUM3RCxJQUFJLENBQUMsZUFBZSxHQUFHLGFBQWEsQ0FBQztJQUN2QyxDQUFDO0lBRU0sV0FBVztRQUNoQixPQUFPLE9BQU8sQ0FBQyxTQUFTLENBQUMsR0FBRyxDQUFDLGVBQWUsQ0FBQyxJQUFJLENBQUMsZUFBZSxDQUFDLENBQUM7SUFDckUsQ0FBQztJQUVNLFNBQVM7UUFDZCxPQUFPLE9BQU8sQ0FBQyxTQUFTLENBQUMsR0FBRyxDQUFDLG1CQUFtQixDQUFDLElBQUksQ0FBQyxlQUFlLEVBQUUsV0FBVyxDQUFDLENBQUM7SUFDdEYsQ0FBQztDQUNGIn0=
@@ -1,10 +1,20 @@
1
- /// <reference types="node-forge" />
2
1
  import * as plugins from './smartcrypto.plugins.js';
3
2
  export declare class PublicKey {
4
3
  static createFromPrivateKey(pemString: string): PublicKey;
5
4
  static fromPemString(pemString: string): PublicKey;
6
- forgePublicKey: plugins.nodeForge.pki.PublicKey;
7
- constructor(publicKeyArg: plugins.nodeForge.pki.PublicKey);
5
+ /**
6
+ * Parses exactly one canonical SPKI or PKCS#1 RSA public-key PEM block and
7
+ * requires a modulus of at least 2048 bits.
8
+ */
9
+ static fromCanonicalRsaPublicPemString(pemString: string): PublicKey;
10
+ forgePublicKey: plugins.nodeForge.pki.rsa.PublicKey;
11
+ constructor(publicKeyArg: plugins.nodeForge.pki.rsa.PublicKey);
8
12
  toPemString(): string;
13
+ /**
14
+ * Verifies RSASSA-PKCS1-v1_5 with an exact SHA-256 DigestInfo. The exact
15
+ * encoding check prevents accepting signatures that label the digest with a
16
+ * different algorithm identifier.
17
+ */
18
+ verifyRs256(dataArg: Uint8Array, signatureArg: Uint8Array): boolean;
9
19
  toOpenSSH(): string;
10
20
  }
@@ -1,4 +1,5 @@
1
1
  import * as plugins from './smartcrypto.plugins.js';
2
+ const canonicalPublicKeyError = (messageArg) => new Error(`Invalid canonical RSA public key: ${messageArg}`);
2
3
  export class PublicKey {
3
4
  // STATIC
4
5
  static createFromPrivateKey(pemString) {
@@ -8,14 +9,93 @@ export class PublicKey {
8
9
  static fromPemString(pemString) {
9
10
  return new PublicKey(plugins.nodeForge.pki.publicKeyFromPem(pemString));
10
11
  }
12
+ /**
13
+ * Parses exactly one canonical SPKI or PKCS#1 RSA public-key PEM block and
14
+ * requires a modulus of at least 2048 bits.
15
+ */
16
+ static fromCanonicalRsaPublicPemString(pemString) {
17
+ if (typeof pemString !== 'string' || pemString.length < 1 || pemString.length > 32_768) {
18
+ throw canonicalPublicKeyError('expected one bounded PEM block');
19
+ }
20
+ const normalizedPem = pemString.trim();
21
+ const spkiMatch = /^-----BEGIN PUBLIC KEY-----\r?\n([A-Za-z0-9+/=\r\n]+)\r?\n-----END PUBLIC KEY-----$/.exec(normalizedPem);
22
+ const pkcs1Match = /^-----BEGIN RSA PUBLIC KEY-----\r?\n([A-Za-z0-9+/=\r\n]+)\r?\n-----END RSA PUBLIC KEY-----$/.exec(normalizedPem);
23
+ const publicKeyMatch = spkiMatch || pkcs1Match;
24
+ if (!publicKeyMatch) {
25
+ throw canonicalPublicKeyError('expected one public-key PEM block');
26
+ }
27
+ const flattenedBase64 = publicKeyMatch[1].replace(/[\r\n]/g, '');
28
+ if (flattenedBase64.length < 1
29
+ || flattenedBase64.length % 4 !== 0
30
+ || !/^[A-Za-z0-9+/]+={0,2}$/.test(flattenedBase64)) {
31
+ throw canonicalPublicKeyError('PEM body is malformed');
32
+ }
33
+ try {
34
+ const pemMessages = plugins.nodeForge.pem.decode(normalizedPem);
35
+ if (pemMessages.length !== 1) {
36
+ throw canonicalPublicKeyError('expected exactly one decoded PEM block');
37
+ }
38
+ const pemMessage = pemMessages[0];
39
+ if (plugins.nodeForge.util.encode64(pemMessage.body) !== flattenedBase64) {
40
+ throw canonicalPublicKeyError('PEM body is not canonical base64');
41
+ }
42
+ const publicKeyAsn1 = plugins.nodeForge.asn1.fromDer(pemMessage.body, true);
43
+ const publicKey = plugins.nodeForge.pki.publicKeyFromAsn1(publicKeyAsn1);
44
+ if (publicKey.n.bitLength() < 2_048) {
45
+ throw canonicalPublicKeyError('RSA modulus must contain at least 2048 bits');
46
+ }
47
+ const canonicalAsn1 = spkiMatch
48
+ ? plugins.nodeForge.pki.publicKeyToAsn1(publicKey)
49
+ : plugins.nodeForge.pki.publicKeyToRSAPublicKey(publicKey);
50
+ const canonicalDer = plugins.nodeForge.asn1.toDer(canonicalAsn1).getBytes();
51
+ if (canonicalDer !== pemMessage.body) {
52
+ throw canonicalPublicKeyError('public-key DER is not canonical');
53
+ }
54
+ return new PublicKey(publicKey);
55
+ }
56
+ catch (error) {
57
+ if (error instanceof Error && error.message.startsWith('Invalid canonical RSA public key:')) {
58
+ throw error;
59
+ }
60
+ throw canonicalPublicKeyError('PEM does not encode a valid RSA public key');
61
+ }
62
+ }
11
63
  constructor(publicKeyArg) {
12
64
  this.forgePublicKey = publicKeyArg;
13
65
  }
14
66
  toPemString() {
15
67
  return plugins.nodeForge.pki.publicKeyToPem(this.forgePublicKey);
16
68
  }
69
+ /**
70
+ * Verifies RSASSA-PKCS1-v1_5 with an exact SHA-256 DigestInfo. The exact
71
+ * encoding check prevents accepting signatures that label the digest with a
72
+ * different algorithm identifier.
73
+ */
74
+ verifyRs256(dataArg, signatureArg) {
75
+ if (!(dataArg instanceof Uint8Array) || !(signatureArg instanceof Uint8Array)) {
76
+ return false;
77
+ }
78
+ try {
79
+ if (signatureArg.byteLength !== Math.ceil(this.forgePublicKey.n.bitLength() / 8)) {
80
+ return false;
81
+ }
82
+ const messageDigest = plugins.nodeForge.md.sha256.create();
83
+ messageDigest.update(plugins.nodeForge.util.createBuffer(dataArg.slice()).getBytes(), 'raw');
84
+ const digestInfo = plugins.nodeForge.asn1.create(plugins.nodeForge.asn1.Class.UNIVERSAL, plugins.nodeForge.asn1.Type.SEQUENCE, true, [
85
+ plugins.nodeForge.asn1.create(plugins.nodeForge.asn1.Class.UNIVERSAL, plugins.nodeForge.asn1.Type.SEQUENCE, true, [
86
+ plugins.nodeForge.asn1.create(plugins.nodeForge.asn1.Class.UNIVERSAL, plugins.nodeForge.asn1.Type.OID, false, plugins.nodeForge.asn1.oidToDer(plugins.nodeForge.pki.oids.sha256).getBytes()),
87
+ plugins.nodeForge.asn1.create(plugins.nodeForge.asn1.Class.UNIVERSAL, plugins.nodeForge.asn1.Type.NULL, false, ''),
88
+ ]),
89
+ plugins.nodeForge.asn1.create(plugins.nodeForge.asn1.Class.UNIVERSAL, plugins.nodeForge.asn1.Type.OCTETSTRING, false, messageDigest.digest().getBytes()),
90
+ ]);
91
+ return this.forgePublicKey.verify(plugins.nodeForge.asn1.toDer(digestInfo).getBytes(), plugins.nodeForge.util.createBuffer(signatureArg.slice()).getBytes(), 'NONE');
92
+ }
93
+ catch {
94
+ return false;
95
+ }
96
+ }
17
97
  toOpenSSH() {
18
98
  return plugins.nodeForge.ssh.publicKeyToOpenSSH(this.forgePublicKey, 'user@host');
19
99
  }
20
100
  }
21
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRjcnlwdG8uY2xhc3Nlcy5wdWJsaWNrZXkuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9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": false,
5
- "description": "easy crypto methods",
5
+ "description": "A library providing easy methods for cryptographic operations, including key pair generation.",
6
6
  "main": "dist_ts/index.js",
7
7
  "typings": "dist_ts/index.d.ts",
8
8
  "author": "Lossless GmbH",
9
9
  "license": "MIT",
10
- "scripts": {
11
- "test": "(tstest test/ --web)",
12
- "build": "(tsbuild --web)",
13
- "buildDocs": "tsdoc"
14
- },
15
10
  "devDependencies": {
16
- "@git.zone/tsbuild": "^2.1.72",
17
- "@git.zone/tsrun": "^1.2.46",
18
- "@git.zone/tstest": "^1.0.86",
19
- "@push.rocks/tapbundle": "^5.0.15",
20
- "@types/node": "^20.11.17"
11
+ "@git.zone/tsbuild": "^4.4.2",
12
+ "@git.zone/tsrun": "^2.0.5",
13
+ "@git.zone/tstest": "^3.6.6",
14
+ "@types/node": "^26.1.1"
21
15
  },
22
16
  "dependencies": {
23
17
  "@push.rocks/smartpromise": "^4.0.3",
@@ -33,11 +27,32 @@
33
27
  "dist_ts_web/**/*",
34
28
  "assets/**/*",
35
29
  "cli.js",
36
- "npmextra.json",
30
+ ".smartconfig.json",
37
31
  "readme.md"
38
32
  ],
39
33
  "browserslist": [
40
34
  "last 1 chrome versions"
41
35
  ],
42
- "type": "module"
43
- }
36
+ "type": "module",
37
+ "keywords": [
38
+ "cryptography",
39
+ "encryption",
40
+ "security",
41
+ "public key",
42
+ "private key",
43
+ "RSA",
44
+ "key pair generation",
45
+ "node-forge",
46
+ "typescript"
47
+ ],
48
+ "homepage": "https://code.foss.global/push.rocks/smartcrypto",
49
+ "repository": {
50
+ "type": "git",
51
+ "url": "https://code.foss.global/push.rocks/smartcrypto.git"
52
+ },
53
+ "scripts": {
54
+ "test": "(tstest test/ --web)",
55
+ "build": "(tsbuild --web)",
56
+ "buildDocs": "tsdoc"
57
+ }
58
+ }
@@ -0,0 +1 @@
1
+
package/readme.md CHANGED
@@ -1,39 +1,102 @@
1
1
  # @push.rocks/smartcrypto
2
2
  easy crypto methods
3
3
 
4
- ## Availabililty and Links
5
- * [npmjs.org (npm package)](https://www.npmjs.com/package/@push.rocks/smartcrypto)
6
- * [gitlab.com (source)](https://gitlab.com/push.rocks/smartcrypto)
7
- * [github.com (source mirror)](https://github.com/push.rocks/smartcrypto)
8
- * [docs (typedoc)](https://push.rocks.gitlab.io/smartcrypto/)
9
-
10
- ## Status for master
11
-
12
- Status Category | Status Badge
13
- -- | --
14
- GitLab Pipelines | [![pipeline status](https://gitlab.com/push.rocks/smartcrypto/badges/master/pipeline.svg)](https://lossless.cloud)
15
- GitLab Pipline Test Coverage | [![coverage report](https://gitlab.com/push.rocks/smartcrypto/badges/master/coverage.svg)](https://lossless.cloud)
16
- npm | [![npm downloads per month](https://badgen.net/npm/dy/@push.rocks/smartcrypto)](https://lossless.cloud)
17
- Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/push.rocks/smartcrypto)](https://lossless.cloud)
18
- TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x/blue?icon=typescript)](https://lossless.cloud)
19
- node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
20
- Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud)
21
- PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/@push.rocks/smartcrypto)](https://lossless.cloud)
22
- PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@push.rocks/smartcrypto)](https://lossless.cloud)
23
- BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/@push.rocks/smartcrypto)](https://lossless.cloud)
4
+ ## Install
5
+ To install `@push.rocks/smartcrypto`, open a terminal and run the following command:
6
+ ```bash
7
+ npm install @push.rocks/smartcrypto
8
+ ```
9
+ This will add `@push.rocks/smartcrypto` to your project's dependencies.
24
10
 
25
11
  ## Usage
12
+ This guide demonstrates how to use `@push.rocks/smartcrypto` effectively by covering its main functionalities. The module offers straightforward methods for handling cryptographic operations. We'll go through generating keys, encrypting and decrypting data, and converting keys to various formats.
26
13
 
27
- ## Contribution
14
+ ### Setting up your project
15
+ Firstly, ensure you have a TypeScript project configured to use ES Modules. Initialize `@push.rocks/smartcrypto` in your project as shown:
28
16
 
29
- We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
17
+ ```typescript
18
+ import { Smartcrypto } from '@push.rocks/smartcrypto';
30
19
 
31
- ## Contribution
20
+ const smartCrypto = new Smartcrypto();
21
+ ```
32
22
 
33
- We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
23
+ ### Generating a Key Pair
24
+ Generating a new RSA key pair is a common requirement. Let's see how `@push.rocks/smartcrypto` simplifies this process:
34
25
 
35
- For further information read the linked docs at the top of this readme.
26
+ ```typescript
27
+ (async () => {
28
+ const keyPair = await smartCrypto.createKeyPair();
29
+ console.log('Public Key:', keyPair.publicKey.toPemString());
30
+ console.log('Private Key:', keyPair.privateKey.toPemString());
31
+ })();
32
+ ```
36
33
 
37
- ## Legal
38
- > MIT licensed | **&copy;** [Task Venture Capital GmbH](https://task.vc)
39
- | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
34
+ This asynchronous function generates a new RSA key pair and logs the PEM-formatted public and private keys. This is particularly useful for creating keys for secure communication.
35
+
36
+ ### Working with Public and Private Keys
37
+ You can manipulate public and private keys using their respective classes. For instance, converting a private key to a PEM string for storage or transmission is straightforward:
38
+
39
+ ```typescript
40
+ import { PrivateKey } from '@push.rocks/smartcrypto';
41
+
42
+ // Assuming you already have a privateKey object:
43
+ const privateKeyPemString = privateKey.toPemString();
44
+ console.log(privateKeyPemString);
45
+ ```
46
+
47
+ Similarly, you can create a `PrivateKey` object from a PEM string. This is useful when you retrieve a stored key and need to use it in your application:
48
+
49
+ ```typescript
50
+ const privateKeyFromPem = PrivateKey.fromPemString(privateKeyPemString);
51
+ ```
52
+
53
+ The same methods apply to public keys with the `PublicKey` class:
54
+
55
+ ```typescript
56
+ import { PublicKey } from '@push.rocks/smartcrypto';
57
+
58
+ const publicKeyPemString = publicKey.toPemString();
59
+ console.log(publicKeyPemString);
60
+
61
+ const publicKeyFromPem = PublicKey.fromPemString(publicKeyPemString);
62
+ ```
63
+
64
+ ### Advanced Usage
65
+ Beyond basic key generation and format conversion, `@push.rocks/smartcrypto` leverages `node-forge` for advanced cryptographic operations. You can engage in more complex scenarios such as signing data, verifying signatures, and even using the library for SSH key generation.
66
+
67
+ ```typescript
68
+ // Example: Signing a message with RSA private key
69
+ const message = 'Hello, SmartCrypto!';
70
+ const signedMessage = privateKey.signMessage(message);
71
+ console.log('Signed Message:', signedMessage);
72
+
73
+ // Example: Verifying a signature with the corresponding public key
74
+ const isVerified = publicKey.verifyMessage(message, signedMessage);
75
+ console.log('Is the message verified?', isVerified);
76
+ ```
77
+
78
+ Note: The `.signMessage` and `.verifyMessage` methods are conceptual and may require specific implementation or extension of the base classes provided by `@push.rocks/smartcrypto`.
79
+
80
+ ### Conclusion
81
+ `@push.rocks/smartcrypto` provides an easy-to-use interface for cryptographic operations, significantly reducing the complexity of managing keys and performing secure data transmission. The examples above showcase a subset of what's possible, encouraging you to explore further and integrate cryptography seamlessly into your Node.js applications.
82
+
83
+ For more advanced use cases and customization, refer to the [node-forge](https://github.com/digitalbazaar/forge) documentation. `@push.rocks/smartcrypto` is designed to work smoothly with `node-forge`, enabling you to extend its functionalities according to your project's needs.
84
+
85
+ ## License and Legal Information
86
+
87
+ This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
88
+
89
+ **Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
90
+
91
+ ### Trademarks
92
+
93
+ This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
94
+
95
+ ### Company Information
96
+
97
+ Task Venture Capital GmbH
98
+ Registered at District court Bremen HRB 35230 HB, Germany
99
+
100
+ For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
101
+
102
+ By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
@@ -1,8 +1,8 @@
1
1
  /**
2
- * autocreated commitinfo by @pushrocks/commitinfo
2
+ * autocreated commitinfo by @push.rocks/commitinfo
3
3
  */
4
4
  export const commitinfo = {
5
5
  name: '@push.rocks/smartcrypto',
6
- version: '2.0.4',
7
- description: 'easy crypto methods'
6
+ version: '2.1.0',
7
+ description: 'A library providing easy methods for cryptographic operations, including key pair generation.'
8
8
  }
@@ -12,9 +12,9 @@ export class PrivateKey {
12
12
  }
13
13
 
14
14
  // INSTANCE
15
- public forgePrivateKey: plugins.nodeForge.pki.PrivateKey;
15
+ public forgePrivateKey: plugins.nodeForge.pki.rsa.PrivateKey;
16
16
 
17
- constructor(privateKeyArg: plugins.nodeForge.pki.PrivateKey) {
17
+ constructor(privateKeyArg: plugins.nodeForge.pki.rsa.PrivateKey) {
18
18
  this.forgePrivateKey = privateKeyArg;
19
19
  }
20
20
 
@@ -1,5 +1,8 @@
1
1
  import * as plugins from './smartcrypto.plugins.js';
2
2
 
3
+ const canonicalPublicKeyError = (messageArg: string): Error =>
4
+ new Error(`Invalid canonical RSA public key: ${messageArg}`);
5
+
3
6
  export class PublicKey {
4
7
  // STATIC
5
8
  public static createFromPrivateKey(pemString: string) {
@@ -11,10 +14,71 @@ export class PublicKey {
11
14
  return new PublicKey(plugins.nodeForge.pki.publicKeyFromPem(pemString));
12
15
  }
13
16
 
17
+ /**
18
+ * Parses exactly one canonical SPKI or PKCS#1 RSA public-key PEM block and
19
+ * requires a modulus of at least 2048 bits.
20
+ */
21
+ public static fromCanonicalRsaPublicPemString(pemString: string) {
22
+ if (typeof pemString !== 'string' || pemString.length < 1 || pemString.length > 32_768) {
23
+ throw canonicalPublicKeyError('expected one bounded PEM block');
24
+ }
25
+ const normalizedPem = pemString.trim();
26
+ const spkiMatch =
27
+ /^-----BEGIN PUBLIC KEY-----\r?\n([A-Za-z0-9+/=\r\n]+)\r?\n-----END PUBLIC KEY-----$/.exec(
28
+ normalizedPem,
29
+ );
30
+ const pkcs1Match =
31
+ /^-----BEGIN RSA PUBLIC KEY-----\r?\n([A-Za-z0-9+/=\r\n]+)\r?\n-----END RSA PUBLIC KEY-----$/.exec(
32
+ normalizedPem,
33
+ );
34
+ const publicKeyMatch = spkiMatch || pkcs1Match;
35
+ if (!publicKeyMatch) {
36
+ throw canonicalPublicKeyError('expected one public-key PEM block');
37
+ }
38
+
39
+ const flattenedBase64 = publicKeyMatch[1].replace(/[\r\n]/g, '');
40
+ if (
41
+ flattenedBase64.length < 1
42
+ || flattenedBase64.length % 4 !== 0
43
+ || !/^[A-Za-z0-9+/]+={0,2}$/.test(flattenedBase64)
44
+ ) {
45
+ throw canonicalPublicKeyError('PEM body is malformed');
46
+ }
47
+
48
+ try {
49
+ const pemMessages = plugins.nodeForge.pem.decode(normalizedPem);
50
+ if (pemMessages.length !== 1) {
51
+ throw canonicalPublicKeyError('expected exactly one decoded PEM block');
52
+ }
53
+ const pemMessage = pemMessages[0];
54
+ if (plugins.nodeForge.util.encode64(pemMessage.body) !== flattenedBase64) {
55
+ throw canonicalPublicKeyError('PEM body is not canonical base64');
56
+ }
57
+ const publicKeyAsn1 = plugins.nodeForge.asn1.fromDer(pemMessage.body, true);
58
+ const publicKey = plugins.nodeForge.pki.publicKeyFromAsn1(publicKeyAsn1);
59
+ if (publicKey.n.bitLength() < 2_048) {
60
+ throw canonicalPublicKeyError('RSA modulus must contain at least 2048 bits');
61
+ }
62
+ const canonicalAsn1 = spkiMatch
63
+ ? plugins.nodeForge.pki.publicKeyToAsn1(publicKey)
64
+ : plugins.nodeForge.pki.publicKeyToRSAPublicKey(publicKey);
65
+ const canonicalDer = plugins.nodeForge.asn1.toDer(canonicalAsn1).getBytes();
66
+ if (canonicalDer !== pemMessage.body) {
67
+ throw canonicalPublicKeyError('public-key DER is not canonical');
68
+ }
69
+ return new PublicKey(publicKey);
70
+ } catch (error) {
71
+ if (error instanceof Error && error.message.startsWith('Invalid canonical RSA public key:')) {
72
+ throw error;
73
+ }
74
+ throw canonicalPublicKeyError('PEM does not encode a valid RSA public key');
75
+ }
76
+ }
77
+
14
78
  // INSTANCE
15
- public forgePublicKey: plugins.nodeForge.pki.PublicKey;
79
+ public forgePublicKey: plugins.nodeForge.pki.rsa.PublicKey;
16
80
 
17
- constructor(publicKeyArg: plugins.nodeForge.pki.PublicKey) {
81
+ constructor(publicKeyArg: plugins.nodeForge.pki.rsa.PublicKey) {
18
82
  this.forgePublicKey = publicKeyArg;
19
83
  }
20
84
 
@@ -22,6 +86,68 @@ export class PublicKey {
22
86
  return plugins.nodeForge.pki.publicKeyToPem(this.forgePublicKey);
23
87
  }
24
88
 
89
+ /**
90
+ * Verifies RSASSA-PKCS1-v1_5 with an exact SHA-256 DigestInfo. The exact
91
+ * encoding check prevents accepting signatures that label the digest with a
92
+ * different algorithm identifier.
93
+ */
94
+ public verifyRs256(dataArg: Uint8Array, signatureArg: Uint8Array): boolean {
95
+ if (!(dataArg instanceof Uint8Array) || !(signatureArg instanceof Uint8Array)) {
96
+ return false;
97
+ }
98
+ try {
99
+ if (signatureArg.byteLength !== Math.ceil(this.forgePublicKey.n.bitLength() / 8)) {
100
+ return false;
101
+ }
102
+ const messageDigest = plugins.nodeForge.md.sha256.create();
103
+ messageDigest.update(
104
+ plugins.nodeForge.util.createBuffer(dataArg.slice()).getBytes(),
105
+ 'raw',
106
+ );
107
+ const digestInfo = plugins.nodeForge.asn1.create(
108
+ plugins.nodeForge.asn1.Class.UNIVERSAL,
109
+ plugins.nodeForge.asn1.Type.SEQUENCE,
110
+ true,
111
+ [
112
+ plugins.nodeForge.asn1.create(
113
+ plugins.nodeForge.asn1.Class.UNIVERSAL,
114
+ plugins.nodeForge.asn1.Type.SEQUENCE,
115
+ true,
116
+ [
117
+ plugins.nodeForge.asn1.create(
118
+ plugins.nodeForge.asn1.Class.UNIVERSAL,
119
+ plugins.nodeForge.asn1.Type.OID,
120
+ false,
121
+ plugins.nodeForge.asn1.oidToDer(
122
+ plugins.nodeForge.pki.oids.sha256,
123
+ ).getBytes(),
124
+ ),
125
+ plugins.nodeForge.asn1.create(
126
+ plugins.nodeForge.asn1.Class.UNIVERSAL,
127
+ plugins.nodeForge.asn1.Type.NULL,
128
+ false,
129
+ '',
130
+ ),
131
+ ],
132
+ ),
133
+ plugins.nodeForge.asn1.create(
134
+ plugins.nodeForge.asn1.Class.UNIVERSAL,
135
+ plugins.nodeForge.asn1.Type.OCTETSTRING,
136
+ false,
137
+ messageDigest.digest().getBytes(),
138
+ ),
139
+ ],
140
+ );
141
+ return this.forgePublicKey.verify(
142
+ plugins.nodeForge.asn1.toDer(digestInfo).getBytes(),
143
+ plugins.nodeForge.util.createBuffer(signatureArg.slice()).getBytes(),
144
+ 'NONE',
145
+ );
146
+ } catch {
147
+ return false;
148
+ }
149
+ }
150
+
25
151
  public toOpenSSH(): string {
26
152
  return plugins.nodeForge.ssh.publicKeyToOpenSSH(this.forgePublicKey, 'user@host');
27
153
  }
package/npmextra.json DELETED
@@ -1,18 +0,0 @@
1
- {
2
- "gitzone": {
3
- "projectType": "npm",
4
- "module": {
5
- "githost": "gitlab.com",
6
- "gitscope": "push.rocks",
7
- "gitrepo": "smartcrypto",
8
- "description": "easy crypto methods",
9
- "npmPackagename": "@push.rocks/smartcrypto",
10
- "license": "MIT",
11
- "projectDomain": "push.rocks"
12
- }
13
- },
14
- "npmci": {
15
- "npmGlobalTools": [],
16
- "npmAccessLevel": "public"
17
- }
18
- }