@signature-kit/crypto 0.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.
- package/CHANGELOG.md +7 -0
- package/LICENSE +21 -0
- package/README.md +30 -0
- package/dist/base64.d.ts +3 -0
- package/dist/base64.d.ts.map +1 -0
- package/dist/base64.js +55 -0
- package/dist/config.d.ts +46 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +78 -0
- package/dist/pem.d.ts +3 -0
- package/dist/pem.d.ts.map +1 -0
- package/dist/pem.js +15 -0
- package/dist/pkcs12.d.ts +13 -0
- package/dist/pkcs12.d.ts.map +1 -0
- package/dist/pkcs12.js +471 -0
- package/dist/primitives/aes.d.ts +17 -0
- package/dist/primitives/aes.d.ts.map +1 -0
- package/dist/primitives/aes.js +219 -0
- package/dist/primitives/des.d.ts +23 -0
- package/dist/primitives/des.d.ts.map +1 -0
- package/dist/primitives/des.js +271 -0
- package/dist/primitives/hmac.d.ts +39 -0
- package/dist/primitives/hmac.d.ts.map +1 -0
- package/dist/primitives/hmac.js +146 -0
- package/dist/primitives/padding.d.ts +11 -0
- package/dist/primitives/padding.d.ts.map +1 -0
- package/dist/primitives/padding.js +29 -0
- package/dist/primitives/pbkdf2.d.ts +19 -0
- package/dist/primitives/pbkdf2.d.ts.map +1 -0
- package/dist/primitives/pbkdf2.js +61 -0
- package/dist/primitives/rc2.d.ts +23 -0
- package/dist/primitives/rc2.d.ts.map +1 -0
- package/dist/primitives/rc2.js +192 -0
- package/dist/primitives/sha1.d.ts +15 -0
- package/dist/primitives/sha1.d.ts.map +1 -0
- package/dist/primitives/sha1.js +102 -0
- package/dist/primitives/sha256.d.ts +30 -0
- package/dist/primitives/sha256.d.ts.map +1 -0
- package/dist/primitives/sha256.js +146 -0
- package/dist/primitives/sha512.d.ts +22 -0
- package/dist/primitives/sha512.d.ts.map +1 -0
- package/dist/primitives/sha512.js +197 -0
- package/package.json +61 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 SignatureKit 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 all
|
|
13
|
+
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# @signature-kit/crypto
|
|
2
|
+
|
|
3
|
+
Shared cryptographic primitives for PKCS#12, PEM, base64, hashing, and cipher operations.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
bun add @signature-kit/crypto effect
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Exports
|
|
12
|
+
|
|
13
|
+
- `@signature-kit/crypto/base64`
|
|
14
|
+
- `@signature-kit/crypto/config`
|
|
15
|
+
- `@signature-kit/crypto/pem`
|
|
16
|
+
- `@signature-kit/crypto/pkcs12`
|
|
17
|
+
|
|
18
|
+
## Runtime model
|
|
19
|
+
|
|
20
|
+
SignatureKit packages are Effect-native. Public APIs return typed `Effect.Effect` values; recoverable faults stay in the typed error channel; callers provide required services and layers explicitly at the application boundary.
|
|
21
|
+
|
|
22
|
+
This is a low-level support package: keep its surface narrow and prefer the higher-level SignatureKit packages unless you need the primitive directly.
|
|
23
|
+
|
|
24
|
+
## Version
|
|
25
|
+
|
|
26
|
+
Current npm release line: `0.1.0`.
|
|
27
|
+
|
|
28
|
+
## License
|
|
29
|
+
|
|
30
|
+
MIT. See `LICENSE`.
|
package/dist/base64.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base64.d.ts","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":"AAYA,eAAO,MAAM,aAAa,GAAI,OAAO,UAAU,KAAG,MAajD,CAAC;AAEF,eAAO,MAAM,aAAa,GAAI,QAAQ,MAAM,KAAG,UAAU,CAAC,WAAW,CA4BpE,CAAC"}
|
package/dist/base64.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
const BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
2
|
+
const base64Value = (char) => {
|
|
3
|
+
const code = char.charCodeAt(0);
|
|
4
|
+
if (code >= 65 && code <= 90)
|
|
5
|
+
return code - 65;
|
|
6
|
+
if (code >= 97 && code <= 122)
|
|
7
|
+
return code - 71;
|
|
8
|
+
if (code >= 48 && code <= 57)
|
|
9
|
+
return code + 4;
|
|
10
|
+
if (char === "+")
|
|
11
|
+
return 62;
|
|
12
|
+
if (char === "/")
|
|
13
|
+
return 63;
|
|
14
|
+
return 0;
|
|
15
|
+
};
|
|
16
|
+
export const bytesToBase64 = (bytes) => {
|
|
17
|
+
let output = "";
|
|
18
|
+
for (let offset = 0; offset < bytes.length; offset += 3) {
|
|
19
|
+
const first = bytes[offset] ?? 0;
|
|
20
|
+
const second = bytes[offset + 1] ?? 0;
|
|
21
|
+
const third = bytes[offset + 2] ?? 0;
|
|
22
|
+
const chunk = (first << 16) | (second << 8) | third;
|
|
23
|
+
output += BASE64_ALPHABET[(chunk >> 18) & 0x3f] ?? "";
|
|
24
|
+
output += BASE64_ALPHABET[(chunk >> 12) & 0x3f] ?? "";
|
|
25
|
+
output += offset + 1 < bytes.length ? (BASE64_ALPHABET[(chunk >> 6) & 0x3f] ?? "") : "=";
|
|
26
|
+
output += offset + 2 < bytes.length ? (BASE64_ALPHABET[chunk & 0x3f] ?? "") : "=";
|
|
27
|
+
}
|
|
28
|
+
return output;
|
|
29
|
+
};
|
|
30
|
+
export const base64ToBytes = (base64) => {
|
|
31
|
+
const clean = base64.replace(/[\t\n\r ]/g, "");
|
|
32
|
+
const padding = clean.endsWith("==") ? 2 : clean.endsWith("=") ? 1 : 0;
|
|
33
|
+
const output = new Uint8Array(Math.floor((clean.length * 3) / 4) - padding);
|
|
34
|
+
let outputOffset = 0;
|
|
35
|
+
for (let offset = 0; offset < clean.length; offset += 4) {
|
|
36
|
+
const first = base64Value(clean[offset] ?? "");
|
|
37
|
+
const second = base64Value(clean[offset + 1] ?? "");
|
|
38
|
+
const third = base64Value(clean[offset + 2] ?? "");
|
|
39
|
+
const fourth = base64Value(clean[offset + 3] ?? "");
|
|
40
|
+
const chunk = (first << 18) | (second << 12) | (third << 6) | fourth;
|
|
41
|
+
if (outputOffset < output.length) {
|
|
42
|
+
output[outputOffset] = (chunk >> 16) & 0xff;
|
|
43
|
+
outputOffset++;
|
|
44
|
+
}
|
|
45
|
+
if (outputOffset < output.length) {
|
|
46
|
+
output[outputOffset] = (chunk >> 8) & 0xff;
|
|
47
|
+
outputOffset++;
|
|
48
|
+
}
|
|
49
|
+
if (outputOffset < output.length) {
|
|
50
|
+
output[outputOffset] = chunk & 0xff;
|
|
51
|
+
outputOffset++;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return output;
|
|
55
|
+
};
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @signature-kit/crypto — typed error catalog and the PKCS#12 result contract.
|
|
3
|
+
*
|
|
4
|
+
* The cipher primitives and the PKCS#12 parser construct `CryptoError` at the
|
|
5
|
+
* exact decision point (bad padding, unsupported algorithm, missing material).
|
|
6
|
+
*/
|
|
7
|
+
import { Schema } from "effect";
|
|
8
|
+
export declare const CryptoErrorCodeSchema: Schema.Literals<readonly ["crypto.DECODE_ERROR", "crypto.INVALID_FORMAT", "crypto.UNSUPPORTED_ALGORITHM", "crypto.WRONG_PASSWORD", "crypto.NO_CERTIFICATE", "crypto.NO_PRIVATE_KEY", "crypto.CORRUPTED_FILE", "crypto.CIPHER_ERROR", "crypto.UNKNOWN"]>;
|
|
9
|
+
export type CryptoErrorCode = (typeof CryptoErrorCodeSchema)["Type"];
|
|
10
|
+
export declare const CryptoErrorCodeValue: {
|
|
11
|
+
decodeError: "crypto.DECODE_ERROR";
|
|
12
|
+
invalidFormat: "crypto.INVALID_FORMAT";
|
|
13
|
+
unsupportedAlgorithm: "crypto.UNSUPPORTED_ALGORITHM";
|
|
14
|
+
wrongPassword: "crypto.WRONG_PASSWORD";
|
|
15
|
+
noCertificate: "crypto.NO_CERTIFICATE";
|
|
16
|
+
noPrivateKey: "crypto.NO_PRIVATE_KEY";
|
|
17
|
+
corruptedFile: "crypto.CORRUPTED_FILE";
|
|
18
|
+
cipherError: "crypto.CIPHER_ERROR";
|
|
19
|
+
unknown: "crypto.UNKNOWN";
|
|
20
|
+
};
|
|
21
|
+
export declare const CryptoOperationSchema: Schema.Literals<readonly ["pkcs12.decode", "pkcs12.mac", "pkcs12.decrypt", "cipher.aes", "cipher.des", "cipher.rc2"]>;
|
|
22
|
+
export type CryptoOperation = (typeof CryptoOperationSchema)["Type"];
|
|
23
|
+
export declare const CryptoOperationValue: {
|
|
24
|
+
pkcs12Decode: "pkcs12.decode";
|
|
25
|
+
pkcs12Mac: "pkcs12.mac";
|
|
26
|
+
pkcs12Decrypt: "pkcs12.decrypt";
|
|
27
|
+
cipherAes: "cipher.aes";
|
|
28
|
+
cipherDes: "cipher.des";
|
|
29
|
+
cipherRc2: "cipher.rc2";
|
|
30
|
+
};
|
|
31
|
+
declare const CryptoError_base: Schema.Class<CryptoError, Schema.TaggedStruct<"CryptoError", {
|
|
32
|
+
readonly code: Schema.Literals<readonly ["crypto.DECODE_ERROR", "crypto.INVALID_FORMAT", "crypto.UNSUPPORTED_ALGORITHM", "crypto.WRONG_PASSWORD", "crypto.NO_CERTIFICATE", "crypto.NO_PRIVATE_KEY", "crypto.CORRUPTED_FILE", "crypto.CIPHER_ERROR", "crypto.UNKNOWN"]>;
|
|
33
|
+
readonly reason: Schema.optional<Schema.String>;
|
|
34
|
+
readonly operation: Schema.optional<Schema.Literals<readonly ["pkcs12.decode", "pkcs12.mac", "pkcs12.decrypt", "cipher.aes", "cipher.des", "cipher.rc2"]>>;
|
|
35
|
+
}>, import("effect/Cause").YieldableError>;
|
|
36
|
+
export declare class CryptoError extends CryptoError_base {
|
|
37
|
+
get message(): string;
|
|
38
|
+
}
|
|
39
|
+
export declare const Pkcs12ResultSchema: Schema.Struct<{
|
|
40
|
+
readonly certificate: Schema.Uint8Array;
|
|
41
|
+
readonly privateKey: Schema.Uint8Array;
|
|
42
|
+
readonly chain: Schema.$Array<Schema.Uint8Array>;
|
|
43
|
+
}>;
|
|
44
|
+
export type Pkcs12Result = (typeof Pkcs12ResultSchema)["Type"];
|
|
45
|
+
export {};
|
|
46
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,eAAO,MAAM,qBAAqB,yPAUhC,CAAC;AACH,MAAM,MAAM,eAAe,GAAG,CAAC,OAAO,qBAAqB,CAAC,CAAC,MAAM,CAAC,CAAC;AACrE,eAAO,MAAM,oBAAoB;;;;;;;;;;CAUU,CAAC;AAE5C,eAAO,MAAM,qBAAqB,uHAOhC,CAAC;AACH,MAAM,MAAM,eAAe,GAAG,CAAC,OAAO,qBAAqB,CAAC,CAAC,MAAM,CAAC,CAAC;AACrE,eAAO,MAAM,oBAAoB;;;;;;;CAOU,CAAC;;;;;;AAE5C,qBAAa,WAAY,SAAQ,gBAI/B;IACA,IAAI,OAAO,IAAI,MAAM,CAqBpB;CACF;AAED,eAAO,MAAM,kBAAkB;;;;EAI7B,CAAC;AACH,MAAM,MAAM,YAAY,GAAG,CAAC,OAAO,kBAAkB,CAAC,CAAC,MAAM,CAAC,CAAC"}
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @signature-kit/crypto — typed error catalog and the PKCS#12 result contract.
|
|
3
|
+
*
|
|
4
|
+
* The cipher primitives and the PKCS#12 parser construct `CryptoError` at the
|
|
5
|
+
* exact decision point (bad padding, unsupported algorithm, missing material).
|
|
6
|
+
*/
|
|
7
|
+
import { Schema } from "effect";
|
|
8
|
+
export const CryptoErrorCodeSchema = Schema.Literals([
|
|
9
|
+
"crypto.DECODE_ERROR",
|
|
10
|
+
"crypto.INVALID_FORMAT",
|
|
11
|
+
"crypto.UNSUPPORTED_ALGORITHM",
|
|
12
|
+
"crypto.WRONG_PASSWORD",
|
|
13
|
+
"crypto.NO_CERTIFICATE",
|
|
14
|
+
"crypto.NO_PRIVATE_KEY",
|
|
15
|
+
"crypto.CORRUPTED_FILE",
|
|
16
|
+
"crypto.CIPHER_ERROR",
|
|
17
|
+
"crypto.UNKNOWN",
|
|
18
|
+
]);
|
|
19
|
+
export const CryptoErrorCodeValue = {
|
|
20
|
+
decodeError: "crypto.DECODE_ERROR",
|
|
21
|
+
invalidFormat: "crypto.INVALID_FORMAT",
|
|
22
|
+
unsupportedAlgorithm: "crypto.UNSUPPORTED_ALGORITHM",
|
|
23
|
+
wrongPassword: "crypto.WRONG_PASSWORD",
|
|
24
|
+
noCertificate: "crypto.NO_CERTIFICATE",
|
|
25
|
+
noPrivateKey: "crypto.NO_PRIVATE_KEY",
|
|
26
|
+
corruptedFile: "crypto.CORRUPTED_FILE",
|
|
27
|
+
cipherError: "crypto.CIPHER_ERROR",
|
|
28
|
+
unknown: "crypto.UNKNOWN",
|
|
29
|
+
};
|
|
30
|
+
export const CryptoOperationSchema = Schema.Literals([
|
|
31
|
+
"pkcs12.decode",
|
|
32
|
+
"pkcs12.mac",
|
|
33
|
+
"pkcs12.decrypt",
|
|
34
|
+
"cipher.aes",
|
|
35
|
+
"cipher.des",
|
|
36
|
+
"cipher.rc2",
|
|
37
|
+
]);
|
|
38
|
+
export const CryptoOperationValue = {
|
|
39
|
+
pkcs12Decode: "pkcs12.decode",
|
|
40
|
+
pkcs12Mac: "pkcs12.mac",
|
|
41
|
+
pkcs12Decrypt: "pkcs12.decrypt",
|
|
42
|
+
cipherAes: "cipher.aes",
|
|
43
|
+
cipherDes: "cipher.des",
|
|
44
|
+
cipherRc2: "cipher.rc2",
|
|
45
|
+
};
|
|
46
|
+
export class CryptoError extends Schema.TaggedErrorClass()("CryptoError", {
|
|
47
|
+
code: CryptoErrorCodeSchema,
|
|
48
|
+
reason: Schema.optional(Schema.String),
|
|
49
|
+
operation: Schema.optional(CryptoOperationSchema),
|
|
50
|
+
}) {
|
|
51
|
+
get message() {
|
|
52
|
+
switch (this.code) {
|
|
53
|
+
case "crypto.DECODE_ERROR":
|
|
54
|
+
return this.reason ?? "Failed to decode PKCS#12 ASN.1.";
|
|
55
|
+
case "crypto.INVALID_FORMAT":
|
|
56
|
+
return this.reason ?? "Unsupported PKCS#12 format.";
|
|
57
|
+
case "crypto.UNSUPPORTED_ALGORITHM":
|
|
58
|
+
return this.reason ?? "Unsupported PKCS#12 encryption algorithm.";
|
|
59
|
+
case "crypto.WRONG_PASSWORD":
|
|
60
|
+
return "Wrong PKCS#12 password.";
|
|
61
|
+
case "crypto.NO_CERTIFICATE":
|
|
62
|
+
return "No certificate found in PKCS#12 file.";
|
|
63
|
+
case "crypto.NO_PRIVATE_KEY":
|
|
64
|
+
return "No private key found in PKCS#12 file.";
|
|
65
|
+
case "crypto.CORRUPTED_FILE":
|
|
66
|
+
return this.reason ?? "Corrupted PKCS#12 file.";
|
|
67
|
+
case "crypto.CIPHER_ERROR":
|
|
68
|
+
return this.reason ?? "Cipher operation failed.";
|
|
69
|
+
case "crypto.UNKNOWN":
|
|
70
|
+
return this.reason ?? "Unknown crypto failure.";
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
export const Pkcs12ResultSchema = Schema.Struct({
|
|
75
|
+
certificate: Schema.Uint8Array,
|
|
76
|
+
privateKey: Schema.Uint8Array,
|
|
77
|
+
chain: Schema.Array(Schema.Uint8Array),
|
|
78
|
+
});
|
package/dist/pem.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pem.d.ts","sourceRoot":"","sources":["../src/pem.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,QAAQ,GAAI,KAAK,MAAM,KAAG,UAMpC,CAAC;AAEJ,eAAO,MAAM,QAAQ,GAAI,KAAK,UAAU,EAAE,OAAO,MAAM,KAAG,MASzD,CAAC"}
|
package/dist/pem.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { base64ToBytes, bytesToBase64 } from "./base64";
|
|
2
|
+
export const pemToDer = (pem) => base64ToBytes(pem
|
|
3
|
+
.split(/\r?\n/)
|
|
4
|
+
.filter((line) => !line.startsWith("-----"))
|
|
5
|
+
.join(""));
|
|
6
|
+
export const derToPem = (der, label) => {
|
|
7
|
+
const base64 = bytesToBase64(der);
|
|
8
|
+
const lines = [];
|
|
9
|
+
lines.push(`-----BEGIN ${label}-----`);
|
|
10
|
+
for (let i = 0; i < base64.length; i += 64) {
|
|
11
|
+
lines.push(base64.slice(i, i + 64));
|
|
12
|
+
}
|
|
13
|
+
lines.push(`-----END ${label}-----`);
|
|
14
|
+
return lines.join("\n");
|
|
15
|
+
};
|
package/dist/pkcs12.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PKCS#12 (.pfx/.p12) parser — fully Effect-native.
|
|
3
|
+
*
|
|
4
|
+
* Navigates the ASN.1 structure with the typed `@signature-kit/asn1` accessors, so
|
|
5
|
+
* every read narrows without a cast. The MAC check is the password gate; once it
|
|
6
|
+
* passes, a later cipher failure is treated as a corrupt file. All failures are
|
|
7
|
+
* `CryptoError` constructed at the decision point — nothing throws.
|
|
8
|
+
*/
|
|
9
|
+
import { Effect, Redacted } from "effect";
|
|
10
|
+
import { CryptoError, type Pkcs12Result } from "./config";
|
|
11
|
+
/** Parse a `.pfx`/`.p12` container, returning DER certificate + private key. */
|
|
12
|
+
export declare const parsePkcs12: (data: Uint8Array, password: Redacted.Redacted<string>) => Effect.Effect<Pkcs12Result, CryptoError>;
|
|
13
|
+
//# sourceMappingURL=pkcs12.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pkcs12.d.ts","sourceRoot":"","sources":["../src/pkcs12.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAYH,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAC1C,OAAO,EACL,WAAW,EAGX,KAAK,YAAY,EAClB,MAAM,UAAU,CAAC;AAohBlB,gFAAgF;AAChF,eAAO,MAAM,WAAW,GACtB,MAAM,UAAU,EAChB,UAAU,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAAC,KAClC,MAAM,CAAC,MAAM,CAAC,YAAY,EAAE,WAAW,CAyHvC,CAAC"}
|