@riao/crypto 1.0.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.
package/LICENSE.md ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2026 riao-project and others
2
+
3
+ MIT
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # riao-crypto
2
+
3
+ {{ remrg:task Write a brief project description }}
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install riao-crypto
9
+ ```
10
+
11
+ ## Getting Started
12
+
13
+ Read the [Getting Started Guide](./docs/guides/getting-started.md) to get started.
14
+
15
+ ## Contributing
16
+
17
+ - [Contributing Guide](./CONTRIBUTING.md)
18
+ - [Setup Guide](./docs/guides//setup.md)
19
+ - [Development Guide](./docs/guides/development.md)
20
+
21
+ ## License
22
+
23
+ Licensed under the [MIT](LICENSE.md).
@@ -0,0 +1,4 @@
1
+ //#region __vite-browser-external
2
+ var e=require("./_rolldown/runtime.cjs").__commonJSMin((e,r)=>{r.exports={}});
3
+ //#endregion
4
+ Object.defineProperty(exports,"default",{enumerable:!0,get:function(){return e()}});
@@ -0,0 +1,5 @@
1
+ import{__commonJSMin as r}from"./_rolldown/runtime.mjs";
2
+ //#region __vite-browser-external
3
+ var o=/* @__PURE__ */r((r,o)=>{o.exports={}});
4
+ //#endregion
5
+ export default o();export{o as require___vite_browser_external};
@@ -0,0 +1,4 @@
1
+ //#region \0rolldown/runtime.js
2
+ var e=Object.create,t=Object.defineProperty,r=Object.getOwnPropertyDescriptor,o=Object.getOwnPropertyNames,l=Object.getPrototypeOf,n=Object.prototype.hasOwnProperty;
3
+ //#endregion
4
+ exports.__commonJSMin=(e,t)=>()=>(t||(e((t={exports:{}}).exports,t),e=null),t.exports),exports.__toESM=(p,a,c)=>(c=null!=p?e(l(p)):{},((e,l,p,a)=>{if(l&&"object"==typeof l||"function"==typeof l)for(var c,u=o(l),b=0,O=u.length;b<O;b++)c=u[b],n.call(e,c)||c===p||t(e,c,{get:(e=>l[e]).bind(null,c),enumerable:!(a=r(l,c))||a.enumerable});return e})(!a&&p&&p.__esModule&&n.call(p,"default")?c:t(c,"default",{value:p,enumerable:!0}),p));
@@ -0,0 +1,4 @@
1
+ //#region \0rolldown/runtime.js
2
+ var e=Object.create,t=Object.defineProperty,r=Object.getOwnPropertyDescriptor,o=Object.getOwnPropertyNames,l=Object.getPrototypeOf,n=Object.prototype.hasOwnProperty,p=(e,t)=>()=>(t||(e((t={exports:{}}).exports,t),e=null),t.exports),a=(p,a,c)=>(c=null!=p?e(l(p)):{},((e,l,p,a)=>{if(l&&"object"==typeof l||"function"==typeof l)for(var c,u=o(l),b=0,O=u.length;b<O;b++)c=u[b],n.call(e,c)||c===p||t(e,c,{get:(e=>l[e]).bind(null,c),enumerable:!(a=r(l,c))||a.enumerable});return e})(!a&&p&&p.__esModule&&n.call(p,"default")?c:t(c,"default",{value:p,enumerable:!0}),p));
3
+ //#endregion
4
+ export{p as __commonJSMin,a as __toESM};
@@ -0,0 +1,5 @@
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("./_virtual/_rolldown/runtime.cjs"),e=require("./_virtual/__vite-browser-external.cjs");
2
+ //#region src/crypto.ts
3
+ var t=r.__toESM(e.default,1),o=t.constants.RSA_PKCS1_OAEP_PADDING,p="sha256";function c(r,e){return e=t.Buffer.isBuffer(e)?e:t.Buffer.from(e),t.publicEncrypt({key:r,padding:o,oaepHash:p},e)}function i(r,e){return t.privateDecrypt({key:r,padding:o,oaepHash:p},e)}
4
+ //#endregion
5
+ exports.Decryptor=class{privateKey;constructor(r){this.privateKey=r}decrypt(r){return i(this.privateKey,r)}},exports.Encryptor=class{publicKey;constructor(r){this.publicKey=r}encrypt(r){return c(this.publicKey,r)}},exports.decrypt=i,exports.encrypt=c;
@@ -0,0 +1,14 @@
1
+ import { Buffer } from 'buffer';
2
+ import * as crypto from 'crypto';
3
+ export declare function encrypt(publicKey: crypto.KeyObject, data: string | Buffer): Buffer;
4
+ export declare function decrypt(privateKey: crypto.KeyObject, encryptedData: Buffer): Buffer;
5
+ export declare class Encryptor {
6
+ private publicKey;
7
+ constructor(publicKey: crypto.KeyObject);
8
+ encrypt(data: string | Buffer): Buffer;
9
+ }
10
+ export declare class Decryptor {
11
+ private privateKey;
12
+ constructor(privateKey: crypto.KeyObject);
13
+ decrypt(encryptedData: Buffer): Buffer;
14
+ }
@@ -0,0 +1,5 @@
1
+ import{__toESM as r}from"./_virtual/_rolldown/runtime.mjs";import{require___vite_browser_external as t}from"./_virtual/__vite-browser-external.mjs";
2
+ //#region src/crypto.ts
3
+ var e=/* @__PURE__ */r(t(),1),i=e.constants.RSA_PKCS1_OAEP_PADDING,a="sha256";function n(r,t){return t=e.Buffer.isBuffer(t)?t:e.Buffer.from(t),e.publicEncrypt({key:r,padding:i,oaepHash:a},t)}function p(r,t){return e.privateDecrypt({key:r,padding:i,oaepHash:a},t)}var s=class{publicKey;constructor(r){this.publicKey=r}encrypt(r){return n(this.publicKey,r)}},o=class{privateKey;constructor(r){this.privateKey=r}decrypt(r){return p(this.privateKey,r)}};
4
+ //#endregion
5
+ export{o as Decryptor,s as Encryptor,p as decrypt,n as encrypt};
@@ -0,0 +1,4 @@
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});let e=require("bcrypt");
2
+ //#region src/hash.ts
3
+ //#endregion
4
+ exports.Hash=class{rounds=12;async make(r,a){return await(0,e.hash)(r,a||this.rounds)}async check(r,a){return await(0,e.compare)(r,a)}};
@@ -0,0 +1,5 @@
1
+ export declare class Hash {
2
+ rounds: number;
3
+ make(input: string, rounds?: number): Promise<string>;
4
+ check(input: string, hashed: string): Promise<boolean>;
5
+ }
@@ -0,0 +1,5 @@
1
+ import{compare as r,hash as a}from"bcrypt";
2
+ //#region src/hash.ts
3
+ var t=class{rounds=12;async make(r,t){return await a(r,t||this.rounds)}async check(a,t){return await r(a,t)}};
4
+ //#endregion
5
+ export{t as Hash};
@@ -0,0 +1 @@
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("./crypto.cjs"),e=require("./hash.cjs"),t=require("./jwt.cjs"),o=require("./keypair.cjs");exports.Decryptor=r.Decryptor,exports.Encryptor=r.Encryptor,exports.Hash=e.Hash,exports.Jwt=t.Jwt,exports.KeyPairGenerator=o.KeyPairGenerator,exports.decrypt=r.decrypt,exports.encrypt=r.encrypt;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Export the public library here.
3
+ *
4
+ * See docs/guides/public-exports.md for
5
+ * detailed conventions on naming, export patterns,
6
+ * and package.json configuration.
7
+ */
8
+ export * from './crypto';
9
+ export * from './hash';
10
+ export * from './jwt';
11
+ export * from './keypair';
12
+ export * from './secret';
@@ -0,0 +1 @@
1
+ import{Decryptor as m,Encryptor as r,decrypt as o,encrypt as p}from"./crypto.mjs";import{Hash as t}from"./hash.mjs";import{Jwt as i}from"./jwt.mjs";import{KeyPairGenerator as j}from"./keypair.mjs";export{m as Decryptor,r as Encryptor,t as Hash,i as Jwt,j as KeyPairGenerator,o as decrypt,p as encrypt};
@@ -0,0 +1,5 @@
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./_virtual/_rolldown/runtime.cjs"),t=require("./_virtual/__vite-browser-external.cjs");let r=require("jsonwebtoken");r=e.__toESM(r,1);
2
+ //#region src/jwt.ts
3
+ var i=e.__toESM(t.default,1);
4
+ //#endregion
5
+ exports.Jwt=class{publicKey;privateKey;expiresIn;notBefore;algorithm;constructor(e){if(this.expiresIn=e.expiresIn??this.expiresIn,this.notBefore=e.notBefore??this.notBefore,this.algorithm=e.algorithm??("secret"in e?"HS512":"ES512"),"secret"in e&&e.secret)this.privateKey=this.publicKey=(0,i.createSecretKey)(i.Buffer.from(e.secret));else{if(!("privateKey"in e)||!e.privateKey)throw new Error("No valid signing key provided. Please pass a secret or a key pair");this.publicKey=e.publicKey,this.privateKey=e.privateKey}}tokenOptions(){return{expiresIn:this.expiresIn??"15m",notBefore:this.notBefore??"1s",algorithm:this.algorithm}}async generateToken(e,t={}){return t={...this.tokenOptions(),...t},{token:await r.sign(e,this.privateKey,t)}}async decodeToken(e){const t=await r.verify(e,this.publicKey,{algorithms:[this.algorithm]});if("string"==typeof t)throw new Error("Invalid token: payload is a string");return delete t.iat,delete t.nbf,delete t.exp,t}};
@@ -0,0 +1,25 @@
1
+ import { default as ms } from 'ms';
2
+ import { Secret, SecretAlgorithm } from './secret';
3
+ import { KeyPair, KeyPairAlgorithm } from './keypair';
4
+ import { KeyObject } from 'crypto';
5
+ import * as jwt from 'jsonwebtoken';
6
+ export type JwtPayload = Record<string, unknown>;
7
+ export type JwtSigningOptions = Secret | KeyPair;
8
+ export type JwtOptions = JwtSigningOptions & {
9
+ expiresIn?: ms.StringValue;
10
+ notBefore?: ms.StringValue;
11
+ };
12
+ export interface Token {
13
+ token: string;
14
+ }
15
+ export declare class Jwt<TPayload extends JwtPayload = JwtPayload> {
16
+ protected publicKey: KeyObject;
17
+ protected privateKey: KeyObject;
18
+ protected expiresIn?: ms.StringValue;
19
+ protected notBefore?: ms.StringValue;
20
+ protected algorithm: SecretAlgorithm | KeyPairAlgorithm;
21
+ constructor(options: JwtOptions);
22
+ protected tokenOptions(): jwt.SignOptions;
23
+ generateToken(data: TPayload, options?: jwt.SignOptions): Promise<Token>;
24
+ decodeToken(token: string): Promise<TPayload>;
25
+ }
@@ -0,0 +1,5 @@
1
+ import{__toESM as e}from"./_virtual/_rolldown/runtime.mjs";import{require___vite_browser_external as t}from"./_virtual/__vite-browser-external.mjs";import*as r from"jsonwebtoken";
2
+ //#region src/jwt.ts
3
+ var i=/* @__PURE__ */e(t(),1),o=class{publicKey;privateKey;expiresIn;notBefore;algorithm;constructor(e){if(this.expiresIn=e.expiresIn??this.expiresIn,this.notBefore=e.notBefore??this.notBefore,this.algorithm=e.algorithm??("secret"in e?"HS512":"ES512"),"secret"in e&&e.secret)this.privateKey=this.publicKey=(0,i.createSecretKey)(i.Buffer.from(e.secret));else{if(!("privateKey"in e)||!e.privateKey)throw new Error("No valid signing key provided. Please pass a secret or a key pair");this.publicKey=e.publicKey,this.privateKey=e.privateKey}}tokenOptions(){return{expiresIn:this.expiresIn??"15m",notBefore:this.notBefore??"1s",algorithm:this.algorithm}}async generateToken(e,t={}){return t={...this.tokenOptions(),...t},{token:await r.sign(e,this.privateKey,t)}}async decodeToken(e){const t=await r.verify(e,this.publicKey,{algorithms:[this.algorithm]});if("string"==typeof t)throw new Error("Invalid token: payload is a string");return delete t.iat,delete t.nbf,delete t.exp,t}};
4
+ //#endregion
5
+ export{o as Jwt};
@@ -0,0 +1,5 @@
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./_virtual/_rolldown/runtime.cjs"),t=require("./_virtual/__vite-browser-external.cjs");
2
+ //#region src/keypair.ts
3
+ var r=e.__toESM(t.default,1);
4
+ //#endregion
5
+ exports.KeyPairGenerator=class{algorithm;constructor(e){this.algorithm=e.algorithm}generate(){let e=null,t={};if("RS256"===this.algorithm?(e="rsa",t.modulusLength=2048):"RS384"===this.algorithm?(e="rsa",t.modulusLength=3072):"RS512"===this.algorithm?(e="rsa",t.modulusLength=4096):"ES256"===this.algorithm?(e="ec",t.namedCurve="prime256v1"):"ES384"===this.algorithm?(e="ec",t.namedCurve="secp384r1"):"ES512"===this.algorithm&&(e="ec",t.namedCurve="secp521r1"),null===e)throw new Error("Keypair type could not be inferred");const{publicKey:i,privateKey:a}=r.generateKeyPairSync(e,t);return{algorithm:this.algorithm,publicKey:i,privateKey:a}}async load(e){const t=(await(0,r.readFile)(e.publicKeyPath)).toString(),i=(await(0,r.readFile)(e.privateKeyPath)).toString();return{algorithm:this.algorithm,publicKey:r.createPublicKey(t),privateKey:r.createPrivateKey(i)}}async save(e){const t=e.keys.publicKey,i=e.keys.privateKey;await(0,r.writeFile)(e.publicKeyPath,t.export({format:"pem",type:"spki"})),await(0,r.writeFile)(e.privateKeyPath,i.export({format:"pem",type:"pkcs8"}))}};
@@ -0,0 +1,23 @@
1
+ import * as crypto from 'crypto';
2
+ export type KeyPairAlgorithm = 'RS256' | 'RS384' | 'RS512' | 'ES256' | 'ES384' | 'ES512';
3
+ export interface KeyPair {
4
+ publicKey: crypto.KeyObject;
5
+ privateKey: crypto.KeyObject;
6
+ algorithm: KeyPairAlgorithm;
7
+ }
8
+ export declare class KeyPairGenerator {
9
+ algorithm: KeyPairAlgorithm;
10
+ constructor(options: {
11
+ algorithm: KeyPairAlgorithm;
12
+ });
13
+ generate(): KeyPair;
14
+ load(options: {
15
+ publicKeyPath: string;
16
+ privateKeyPath: string;
17
+ }): Promise<KeyPair>;
18
+ save(options: {
19
+ keys: KeyPair;
20
+ publicKeyPath: string;
21
+ privateKeyPath: string;
22
+ }): Promise<void>;
23
+ }
@@ -0,0 +1,5 @@
1
+ import{__toESM as e}from"./_virtual/_rolldown/runtime.mjs";import{require___vite_browser_external as t}from"./_virtual/__vite-browser-external.mjs";
2
+ //#region src/keypair.ts
3
+ var r=/* @__PURE__ */e(t(),1),i=class{algorithm;constructor(e){this.algorithm=e.algorithm}generate(){let e=null,t={};if("RS256"===this.algorithm?(e="rsa",t.modulusLength=2048):"RS384"===this.algorithm?(e="rsa",t.modulusLength=3072):"RS512"===this.algorithm?(e="rsa",t.modulusLength=4096):"ES256"===this.algorithm?(e="ec",t.namedCurve="prime256v1"):"ES384"===this.algorithm?(e="ec",t.namedCurve="secp384r1"):"ES512"===this.algorithm&&(e="ec",t.namedCurve="secp521r1"),null===e)throw new Error("Keypair type could not be inferred");const{publicKey:i,privateKey:a}=r.generateKeyPairSync(e,t);return{algorithm:this.algorithm,publicKey:i,privateKey:a}}async load(e){const t=(await(0,r.readFile)(e.publicKeyPath)).toString(),i=(await(0,r.readFile)(e.privateKeyPath)).toString();return{algorithm:this.algorithm,publicKey:r.createPublicKey(t),privateKey:r.createPrivateKey(i)}}async save(e){const t=e.keys.publicKey,i=e.keys.privateKey;await(0,r.writeFile)(e.publicKeyPath,t.export({format:"pem",type:"spki"})),await(0,r.writeFile)(e.privateKeyPath,i.export({format:"pem",type:"pkcs8"}))}};
4
+ //#endregion
5
+ export{i as KeyPairGenerator};
File without changes
@@ -0,0 +1,5 @@
1
+ export type SecretAlgorithm = 'HS256' | 'HS384' | 'HS512';
2
+ export interface Secret {
3
+ secret: string;
4
+ algorithm: SecretAlgorithm;
5
+ }
File without changes
File without changes
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ export declare function setup(): Promise<void>;
2
+ export declare function teardown(): Promise<void>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ declare const _default: import('vite').UserConfig;
2
+ export default _default;
@@ -0,0 +1,2 @@
1
+ declare const _default: import('vite').UserConfig;
2
+ export default _default;
package/package.json ADDED
@@ -0,0 +1,73 @@
1
+ {
2
+ "name": "@riao/crypto",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "author": "riao-project",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "scripts": {
9
+ "build": "vite build",
10
+ "dev": "vite build --watch",
11
+ "prepare": "npm run build",
12
+ "preview": "vite preview",
13
+ "typecheck": "tsc -p tsconfig.json --noEmit",
14
+ "lint": "eslint . --fix && prettier --write .",
15
+ "lint:check": "prettier --check . && eslint .",
16
+ "test": "npm run lint && npm run typecheck && npm run test:run",
17
+ "test:ci": "npm run lint:check && npm run typecheck && npm run test:run",
18
+ "test:dev": "vitest --ui --coverage",
19
+ "test:run": "vitest run --coverage"
20
+ },
21
+ "engines": {
22
+ "node": ">=22.0.0",
23
+ "npm": ">=10.8.2"
24
+ },
25
+ "dependencies": {
26
+ "bcrypt": "^6.0.0",
27
+ "jsonwebtoken": "^9.0.3",
28
+ "ms": "^2.1.3"
29
+ },
30
+ "devDependencies": {
31
+ "@eslint/js": "^10.0.1",
32
+ "@types/bcrypt": "^6.0.0",
33
+ "@types/jsonwebtoken": "^9.0.10",
34
+ "@types/ms": "^2.1.0",
35
+ "@types/node": "^22.19.17",
36
+ "@typescript-eslint/eslint-plugin": "^8.57.2",
37
+ "@typescript-eslint/parser": "^8.57.2",
38
+ "@vitest/coverage-v8": "^4.1.2",
39
+ "@vitest/ui": "^4.1.2",
40
+ "eslint": "^10.1.0",
41
+ "fast-glob": "^3.3.3",
42
+ "globals": "^17.6.0",
43
+ "prettier": "^3.8.1",
44
+ "prettier-plugin-brace-style": "^0.10.1",
45
+ "terser": "^5.46.1",
46
+ "typescript": "^5.9.3",
47
+ "vite": "^8.0.9",
48
+ "vite-plugin-dts": "^4.5.4",
49
+ "vitest": "^4.1.2"
50
+ },
51
+ "keywords": [],
52
+ "main": "./dist/src/index.cjs",
53
+ "module": "./dist/src/index.mjs",
54
+ "types": "./dist/src/index.d.ts",
55
+ "exports": {
56
+ "./package.json": "./package.json",
57
+ ".": {
58
+ "types": "./dist/src/index.d.ts",
59
+ "import": "./dist/src/index.mjs",
60
+ "require": "./dist/src/index.cjs",
61
+ "default": "./dist/src/index.mjs"
62
+ },
63
+ "./*": {
64
+ "types": "./dist/src/*/index.d.ts",
65
+ "import": "./dist/src/*/index.mjs",
66
+ "require": "./dist/src/*/index.cjs",
67
+ "default": "./dist/src/*/index.mjs"
68
+ }
69
+ },
70
+ "files": [
71
+ "dist"
72
+ ]
73
+ }