@blazetrails/bcrypt 0.0.0-stage → 0.1.0-alpha0.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/LICENSE +21 -0
- package/dist/constants.generated.d.ts +2 -0
- package/dist/constants.generated.js +15 -0
- package/dist/constants.generated.js.map +1 -0
- package/dist/engine.d.ts +20 -0
- package/dist/engine.js +122 -0
- package/dist/engine.js.map +1 -0
- package/dist/error.d.ts +19 -0
- package/dist/error.js +19 -0
- package/dist/error.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/password.d.ts +16 -0
- package/dist/password.js +42 -0
- package/dist/password.js.map +1 -0
- package/package.json +46 -3
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Dean Marano and Trails contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { defineConstants } from "@blazetrails/ruby-compat";
|
|
2
|
+
import * as m0 from "./engine.js";
|
|
3
|
+
import * as m1 from "./error.js";
|
|
4
|
+
import * as m2 from "./password.js";
|
|
5
|
+
defineConstants([
|
|
6
|
+
[m0, "Engine", "BCrypt::Engine"],
|
|
7
|
+
[m1, "Error", "BCrypt::Error"],
|
|
8
|
+
[m1, "Errors", "BCrypt::Errors"],
|
|
9
|
+
[m1, "InvalidCost", "BCrypt::Errors::InvalidCost"],
|
|
10
|
+
[m1, "InvalidHash", "BCrypt::Errors::InvalidHash"],
|
|
11
|
+
[m1, "InvalidSalt", "BCrypt::Errors::InvalidSalt"],
|
|
12
|
+
[m1, "InvalidSecret", "BCrypt::Errors::InvalidSecret"],
|
|
13
|
+
[m2, "Password", "BCrypt::Password"],
|
|
14
|
+
]);
|
|
15
|
+
//# sourceMappingURL=constants.generated.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.generated.js","sourceRoot":"","sources":["../lib/constants.generated.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAC3D,OAAO,KAAK,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,KAAK,EAAE,MAAM,YAAY,CAAC;AACjC,OAAO,KAAK,EAAE,MAAM,eAAe,CAAC;AAEpC,eAAe,CAAC;IACd,CAAC,EAAE,EAAE,QAAQ,EAAE,gBAAgB,CAAC;IAChC,CAAC,EAAE,EAAE,OAAO,EAAE,eAAe,CAAC;IAC9B,CAAC,EAAE,EAAE,QAAQ,EAAE,gBAAgB,CAAC;IAChC,CAAC,EAAE,EAAE,aAAa,EAAE,6BAA6B,CAAC;IAClD,CAAC,EAAE,EAAE,aAAa,EAAE,6BAA6B,CAAC;IAClD,CAAC,EAAE,EAAE,aAAa,EAAE,6BAA6B,CAAC;IAClD,CAAC,EAAE,EAAE,eAAe,EAAE,+BAA+B,CAAC;IACtD,CAAC,EAAE,EAAE,UAAU,EAAE,kBAAkB,CAAC;CACrC,CAAC,CAAC"}
|
package/dist/engine.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Range } from "@blazetrails/ruby-compat";
|
|
2
|
+
export declare class Engine {
|
|
3
|
+
static DEFAULT_COST: number;
|
|
4
|
+
static readonly MIN_COST: number;
|
|
5
|
+
static readonly MAX_COST: number;
|
|
6
|
+
static readonly MAX_SECRET_BYTESIZE: number;
|
|
7
|
+
static readonly MAX_SALT_LENGTH: number;
|
|
8
|
+
private static _cost;
|
|
9
|
+
static get cost(): number;
|
|
10
|
+
static set cost(cost: number | null);
|
|
11
|
+
static hashSecret(secret: unknown, salt: string | null, _?: unknown): string | null;
|
|
12
|
+
static generateSalt(cost?: number): string | null;
|
|
13
|
+
static isValidSalt(salt: string | null): boolean;
|
|
14
|
+
static isValidSecret(secret: unknown): boolean;
|
|
15
|
+
private static __bcSalt;
|
|
16
|
+
private static __bcCrypt;
|
|
17
|
+
static calibrate(upperTimeLimitInMs: number): number | Range<number>;
|
|
18
|
+
static autodetectCost(salt: string): number;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=engine.d.ts.map
|
package/dist/engine.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import bcryptjs from "bcryptjs";
|
|
2
|
+
import { Time } from "@blazetrails/date";
|
|
3
|
+
import { ArgumentError, OpenSSL, Range, rbObjAsString, rbObjRespondTo, toI, warn, } from "@blazetrails/ruby-compat";
|
|
4
|
+
import { Errors } from "./error.js";
|
|
5
|
+
import { Password } from "./password.js";
|
|
6
|
+
/** @perf 1.6x — PERMANENT */
|
|
7
|
+
const _bcHashSync = (() => {
|
|
8
|
+
const getBuiltinModule = globalThis
|
|
9
|
+
.process?.getBuiltinModule;
|
|
10
|
+
if (getBuiltinModule === undefined)
|
|
11
|
+
return bcryptjs.hashSync;
|
|
12
|
+
const { createRequire } = getBuiltinModule("node:module");
|
|
13
|
+
const { Buffer } = getBuiltinModule("node:buffer");
|
|
14
|
+
let native;
|
|
15
|
+
try {
|
|
16
|
+
native = createRequire(import.meta.url)("bcrypt");
|
|
17
|
+
}
|
|
18
|
+
catch (e) {
|
|
19
|
+
if (e.code !== "MODULE_NOT_FOUND")
|
|
20
|
+
throw e;
|
|
21
|
+
return bcryptjs.hashSync;
|
|
22
|
+
}
|
|
23
|
+
return (key, setting) => {
|
|
24
|
+
const bytes = Buffer.from(key, "utf8").subarray(0, 72);
|
|
25
|
+
if (!setting.startsWith("$2y$"))
|
|
26
|
+
return native.hashSync(bytes, setting);
|
|
27
|
+
return "$2y$" + native.hashSync(bytes, "$2b$" + setting.slice(4)).slice(4);
|
|
28
|
+
};
|
|
29
|
+
})();
|
|
30
|
+
export class Engine {
|
|
31
|
+
static DEFAULT_COST = 12;
|
|
32
|
+
static MIN_COST = 4;
|
|
33
|
+
static MAX_COST = 31;
|
|
34
|
+
static MAX_SECRET_BYTESIZE = 72;
|
|
35
|
+
static MAX_SALT_LENGTH = 16;
|
|
36
|
+
static _cost = null;
|
|
37
|
+
static get cost() {
|
|
38
|
+
return this._cost ?? this.DEFAULT_COST;
|
|
39
|
+
}
|
|
40
|
+
static set cost(cost) {
|
|
41
|
+
this._cost = cost;
|
|
42
|
+
}
|
|
43
|
+
static hashSecret(secret, salt, _ = null) {
|
|
44
|
+
if (_ != null) {
|
|
45
|
+
warn("[DEPRECATION] Passing the third argument to " +
|
|
46
|
+
"`BCrypt::Engine.hash_secret` is deprecated. " +
|
|
47
|
+
"Please do not pass the third argument which " +
|
|
48
|
+
"is currently not used.");
|
|
49
|
+
}
|
|
50
|
+
if (this.isValidSecret(secret)) {
|
|
51
|
+
if (this.isValidSalt(salt)) {
|
|
52
|
+
secret = rbObjAsString(secret);
|
|
53
|
+
return this.__bcCrypt(secret, salt);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
throw new Errors.InvalidSalt("invalid salt");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
throw new Errors.InvalidSecret("invalid secret");
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
static generateSalt(cost = this.cost) {
|
|
64
|
+
cost = Math.trunc(cost);
|
|
65
|
+
if (cost > 0) {
|
|
66
|
+
if (cost < this.MIN_COST) {
|
|
67
|
+
cost = this.MIN_COST;
|
|
68
|
+
}
|
|
69
|
+
return this.__bcSalt("$2a$", cost, OpenSSL.Random.randomBytes(this.MAX_SALT_LENGTH));
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
throw new Errors.InvalidCost("cost must be numeric and > 0");
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
static isValidSalt(salt) {
|
|
76
|
+
return salt != null && /^\$[0-9a-z]{2,}\$[0-9]{2,}\$[A-Za-z0-9./]{22,}$/.test(salt);
|
|
77
|
+
}
|
|
78
|
+
static isValidSecret(secret) {
|
|
79
|
+
return rbObjRespondTo(secret, "toString");
|
|
80
|
+
}
|
|
81
|
+
static __bcSalt(prefix, count, input) {
|
|
82
|
+
const size = input == null ? 0 : input.length;
|
|
83
|
+
if (size < 16 ||
|
|
84
|
+
(count && (count < 4 || count > 31)) ||
|
|
85
|
+
prefix[0] !== "$" ||
|
|
86
|
+
prefix[1] !== "2" ||
|
|
87
|
+
(prefix[2] !== "a" && prefix[2] !== "b" && prefix[2] !== "y")) {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
if (!count)
|
|
91
|
+
count = 5;
|
|
92
|
+
return `$2${prefix[2]}$${Math.floor(count / 10)}${count % 10}$${bcryptjs.encodeBase64(input, 16)}`;
|
|
93
|
+
}
|
|
94
|
+
static __bcCrypt(key, setting) {
|
|
95
|
+
if (key.includes("\0") || setting.includes("\0")) {
|
|
96
|
+
throw new ArgumentError("string contains null byte");
|
|
97
|
+
}
|
|
98
|
+
try {
|
|
99
|
+
return _bcHashSync(key, setting);
|
|
100
|
+
}
|
|
101
|
+
catch (e) {
|
|
102
|
+
if (!(e instanceof Error) || e.constructor !== Error)
|
|
103
|
+
throw e;
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
static calibrate(upperTimeLimitInMs) {
|
|
108
|
+
const costs = new Range(Engine.MIN_COST, Engine.MAX_COST - 1);
|
|
109
|
+
for (const i of costs.each()) {
|
|
110
|
+
const startTime = Time.now();
|
|
111
|
+
Password.create("testing testing", { cost: i + 1 });
|
|
112
|
+
const endTime = Time.now().minus(startTime);
|
|
113
|
+
if (endTime * 1_000 > upperTimeLimitInMs)
|
|
114
|
+
return i;
|
|
115
|
+
}
|
|
116
|
+
return costs;
|
|
117
|
+
}
|
|
118
|
+
static autodetectCost(salt) {
|
|
119
|
+
return toI(salt.slice(4, 6));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
//# sourceMappingURL=engine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"engine.js","sourceRoot":"","sources":["../lib/engine.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,UAAU,CAAC;AAChC,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AACzC,OAAO,EACL,aAAa,EACb,OAAO,EACP,KAAK,EACL,aAAa,EACb,cAAc,EACd,GAAG,EACH,IAAI,GACL,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AACpC,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEzC,6BAA6B;AAC7B,MAAM,WAAW,GAA6C,CAAC,GAAG,EAAE;IAClE,MAAM,gBAAgB,GAAI,UAAuE;SAC9F,OAAO,EAAE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|
|
|
1
|
+
import { StandardError } from "@blazetrails/ruby-compat";
|
|
2
|
+
export declare class Error extends StandardError {
|
|
3
|
+
}
|
|
4
|
+
export declare class InvalidSalt extends Error {
|
|
5
|
+
}
|
|
6
|
+
export declare class InvalidHash extends Error {
|
|
7
|
+
}
|
|
8
|
+
export declare class InvalidCost extends Error {
|
|
9
|
+
}
|
|
10
|
+
export declare class InvalidSecret extends Error {
|
|
11
|
+
}
|
|
12
|
+
export declare const Errors: {
|
|
13
|
+
readonly name: "BCrypt::Errors";
|
|
14
|
+
readonly InvalidSalt: typeof InvalidSalt;
|
|
15
|
+
readonly InvalidHash: typeof InvalidHash;
|
|
16
|
+
readonly InvalidCost: typeof InvalidCost;
|
|
17
|
+
readonly InvalidSecret: typeof InvalidSecret;
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=error.d.ts.map
|
package/dist/error.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { StandardError } from "@blazetrails/ruby-compat";
|
|
2
|
+
export class Error extends StandardError {
|
|
3
|
+
}
|
|
4
|
+
export class InvalidSalt extends Error {
|
|
5
|
+
}
|
|
6
|
+
export class InvalidHash extends Error {
|
|
7
|
+
}
|
|
8
|
+
export class InvalidCost extends Error {
|
|
9
|
+
}
|
|
10
|
+
export class InvalidSecret extends Error {
|
|
11
|
+
}
|
|
12
|
+
export const Errors = {
|
|
13
|
+
name: "BCrypt::Errors",
|
|
14
|
+
InvalidSalt,
|
|
15
|
+
InvalidHash,
|
|
16
|
+
InvalidCost,
|
|
17
|
+
InvalidSecret,
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.js","sourceRoot":"","sources":["../lib/error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAEzD,MAAM,OAAO,KAAM,SAAQ,aAAa;CAAG;AAE3C,MAAM,OAAO,WAAY,SAAQ,KAAK;CAAG;AAEzC,MAAM,OAAO,WAAY,SAAQ,KAAK;CAAG;AAEzC,MAAM,OAAO,WAAY,SAAQ,KAAK;CAAG;AAEzC,MAAM,OAAO,aAAc,SAAQ,KAAK;CAAG;AAE3C,MAAM,CAAC,MAAM,MAAM,GAAG;IACpB,IAAI,EAAE,gBAAgB;IACtB,WAAW;IACX,WAAW;IACX,WAAW;IACX,aAAa;CACL,CAAC"}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { TopLevel } from "@blazetrails/activesupport";
|
|
2
|
+
import { Error, Errors } from "./error.js";
|
|
3
|
+
import { Engine } from "./engine.js";
|
|
4
|
+
import { Password } from "./password.js";
|
|
5
|
+
export { Error, Errors, Engine, Password };
|
|
6
|
+
const BCrypt = { name: "BCrypt" };
|
|
7
|
+
BCrypt.Error = Error;
|
|
8
|
+
BCrypt.Errors = Errors;
|
|
9
|
+
BCrypt.Engine = Engine;
|
|
10
|
+
BCrypt.Password = Password;
|
|
11
|
+
TopLevel.BCrypt = BCrypt;
|
|
12
|
+
import "./constants.generated.js";
|
|
13
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAC;AACtD,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAC3C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEzC,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC;AAE3C,MAAM,MAAM,GAAG,EAAE,IAAI,EAAE,QAAQ,EAM9B,CAAC;AACF,MAAM,CAAC,KAAK,GAAG,KAAK,CAAC;AACrB,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC;AACvB,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC;AACvB,MAAM,CAAC,QAAQ,GAAG,QAAQ,CAAC;AAC3B,QAAQ,CAAC,MAAM,GAAG,MAAM,CAAC;AAEzB,OAAO,0BAA0B,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare class Password extends String {
|
|
2
|
+
readonly checksum: string;
|
|
3
|
+
readonly salt: string;
|
|
4
|
+
readonly version: string;
|
|
5
|
+
readonly cost: number;
|
|
6
|
+
static create(secret: unknown, options?: {
|
|
7
|
+
cost?: number | false | null;
|
|
8
|
+
}): Password;
|
|
9
|
+
static isValidHash(h: unknown): boolean;
|
|
10
|
+
constructor(rawHash: string | Password | null);
|
|
11
|
+
equals(secret: unknown): boolean;
|
|
12
|
+
isPassword(secret: unknown): boolean;
|
|
13
|
+
private isValidHash;
|
|
14
|
+
private splitHash;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=password.d.ts.map
|
package/dist/password.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { ArgumentError, toI } from "@blazetrails/ruby-compat";
|
|
2
|
+
import { Engine } from "./engine.js";
|
|
3
|
+
import { Errors } from "./error.js";
|
|
4
|
+
export class Password extends String {
|
|
5
|
+
checksum;
|
|
6
|
+
salt;
|
|
7
|
+
version;
|
|
8
|
+
cost;
|
|
9
|
+
static create(secret, options = {}) {
|
|
10
|
+
const cost = options.cost != null && options.cost !== false ? options.cost : Engine.cost;
|
|
11
|
+
if (cost > Engine.MAX_COST)
|
|
12
|
+
throw new ArgumentError();
|
|
13
|
+
return new Password(Engine.hashSecret(secret, Engine.generateSalt(cost)));
|
|
14
|
+
}
|
|
15
|
+
static isValidHash(h) {
|
|
16
|
+
return ((typeof h === "string" || h instanceof String) &&
|
|
17
|
+
/^\$[0-9a-z]{2}\$[0-9]{2}\$[A-Za-z0-9./]{53}$/.test(String(h)));
|
|
18
|
+
}
|
|
19
|
+
constructor(rawHash) {
|
|
20
|
+
super(rawHash);
|
|
21
|
+
if (this.isValidHash(rawHash)) {
|
|
22
|
+
[this.version, this.cost, this.salt, this.checksum] = this.splitHash(this);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
throw new Errors.InvalidHash("invalid hash");
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
equals(secret) {
|
|
29
|
+
return this.toString() === Engine.hashSecret(secret, this.salt);
|
|
30
|
+
}
|
|
31
|
+
isPassword(secret) {
|
|
32
|
+
return this.equals(secret);
|
|
33
|
+
}
|
|
34
|
+
isValidHash(h) {
|
|
35
|
+
return this.constructor.isValidHash(h);
|
|
36
|
+
}
|
|
37
|
+
splitHash(h) {
|
|
38
|
+
const [, v, c, mash] = h.split("$");
|
|
39
|
+
return [v, toI(c), h.slice(0, 29), mash.slice(-31)];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=password.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"password.js","sourceRoot":"","sources":["../lib/password.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,GAAG,EAAE,MAAM,0BAA0B,CAAC;AAC9D,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEpC,MAAM,OAAO,QAAS,SAAQ,MAAM;IACzB,QAAQ,CAAS;IACjB,IAAI,CAAS;IACb,OAAO,CAAS;IAChB,IAAI,CAAS;IAEtB,MAAM,CAAC,MAAM,CAAC,MAAe,EAAE,OAAO,GAAqC,EAAE;QAC3E,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,IAAI,IAAI,OAAO,CAAC,IAAI,KAAK,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC;QACzF,IAAI,IAAI,GAAG,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GAAG,CAAC,CAAC;QACpC,OAAO,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAW,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IAChE,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blazetrails/bcrypt",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0-alpha0.1",
|
|
4
|
+
"description": "bcrypt() password hashing, mirroring Ruby's bcrypt gem",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"url": "git+https://github.com/blazetrailsdev/trails.git",
|
|
8
|
+
"directory": "packages/bcrypt"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://github.com/blazetrailsdev/trails/tree/main/packages/bcrypt#readme",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "dist/index.js",
|
|
13
|
+
"types": "dist/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"default": "./dist/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"!dist/**/*.d.ts.map"
|
|
23
|
+
],
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@blazetrails/activesupport": "0.1.0-alpha0.1",
|
|
26
|
+
"@blazetrails/date": "0.1.0-alpha0.1",
|
|
27
|
+
"@blazetrails/ruby-compat": "0.1.0-alpha0.1",
|
|
28
|
+
"bcryptjs": "^3.0.3"
|
|
29
|
+
},
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=26"
|
|
32
|
+
},
|
|
33
|
+
"optionalDependencies": {
|
|
34
|
+
"bcrypt": "^6.0.0"
|
|
35
|
+
},
|
|
36
|
+
"mirrors": [
|
|
37
|
+
{
|
|
38
|
+
"source": "bcrypt-ruby",
|
|
39
|
+
"ref": "v3.1.20",
|
|
40
|
+
"sha": "58f8afd33d8813e84706b938815bb1aa08d0b470"
|
|
41
|
+
}
|
|
42
|
+
],
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "tsc"
|
|
48
|
+
}
|
|
6
49
|
}
|
package/README.md
DELETED