typepki-asn1gen 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/LICENSE +21 -0
- package/README.md +12 -0
- package/dist/import/index.d.mts +51 -0
- package/dist/import/index.d.mts.map +1 -0
- package/dist/import/index.mjs +1 -0
- package/dist/require/index.cjs +1 -0
- package/dist/require/index.d.cts +2 -0
- package/dist/require/index.d.cts.map +1 -0
- package/package.json +68 -0
- package/src/index.cts +1 -0
- package/src/index.mts +95 -0
- package/src/index.test.mts +25 -0
- package/tsconfig.json +21 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Kenji Urushima
|
|
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,12 @@
|
|
|
1
|
+
typepki-asn1gen: ASN.1 data generator for TypePKI library
|
|
2
|
+
=========================================================
|
|
3
|
+
|
|
4
|
+
[TOP](https://kjur.github.io/typepki-asn1gen/) | [github](https://github.com/kjur/typepki-asn1gen) | [npm](https://www.npmjs.com/package/typepki-asn1gen) | [TypePKI](https://kjur.github.com/typepki/)
|
|
5
|
+
|
|
6
|
+
The 'TypePKI' library is an opensource free TypeScript PKI library which is the successor of the long lived [jsrsasign](https://kjur.github.io/jsrsasign) library.
|
|
7
|
+
|
|
8
|
+
The 'typepki-asn1gen' is a ASN.1 encoded data generator sub module for TypePKI library.
|
|
9
|
+
|
|
10
|
+
## FEATURE
|
|
11
|
+
- Dual CommonJS/ES module package supporting CommonJS(CJS) and ES modules
|
|
12
|
+
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ASN.1 data object definition
|
|
3
|
+
* @example
|
|
4
|
+
* let d: ASN1Data = { t: "int", v: "1234" };
|
|
5
|
+
*/
|
|
6
|
+
export interface ASN1Data {
|
|
7
|
+
t: string;
|
|
8
|
+
v: any;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* generate ASN.1 DER/BER encoded hexadecimal string from JSON data
|
|
12
|
+
* @param p - JSON object representing ASN.1 structure
|
|
13
|
+
* @return hexadecimal string of ASN.1 DER/BER encoded hexadecimal string
|
|
14
|
+
* @example
|
|
15
|
+
* getASN1({ t: "int", v: "12ab" }) -> "020212ab"
|
|
16
|
+
* getASN1({ t: "seq", v: [
|
|
17
|
+
* { t: "int", v: "01" },
|
|
18
|
+
* { t: "int", v: "02" }
|
|
19
|
+
* ]}) -> "3006020101020102"
|
|
20
|
+
*/
|
|
21
|
+
export declare function getASN1(p: ASN1Data): string;
|
|
22
|
+
/**
|
|
23
|
+
* convert hexadecimal positive integer to ASN.1 integer value
|
|
24
|
+
* @param h - hexadecimal string of positive integer
|
|
25
|
+
* @return ASN.1 Integer DER encoded hexadecimal string
|
|
26
|
+
* @example
|
|
27
|
+
* pospad("01") -> "01"
|
|
28
|
+
* pospad("ab") -> "00ab"
|
|
29
|
+
* pospad("00000012") -> "12"
|
|
30
|
+
* pospad("000000ab") -> "00ab"
|
|
31
|
+
*/
|
|
32
|
+
export declare function pospad(h: string): string;
|
|
33
|
+
/**
|
|
34
|
+
* get ASN.1 TLV length octet(s) by TLV value octet length
|
|
35
|
+
* @param n - TLV value octet length
|
|
36
|
+
* @return hexadecimal string of TLV length octets
|
|
37
|
+
* @example
|
|
38
|
+
* getLength(3) -> "03"
|
|
39
|
+
* getLength(128) -> "8180"
|
|
40
|
+
*/
|
|
41
|
+
export declare function getLength(n: number): string;
|
|
42
|
+
/**
|
|
43
|
+
* zero padding for hexadecimal string
|
|
44
|
+
* @param s - odd or even length hexadecimal string
|
|
45
|
+
* @return even length zero padded hexadecimal string
|
|
46
|
+
* @example
|
|
47
|
+
* hexpad("1") -> "01"
|
|
48
|
+
* hexpad("ab3c") -> "ab3c"
|
|
49
|
+
*/
|
|
50
|
+
export declare function hexpad(s: string): string;
|
|
51
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/index.mts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,WAAW,QAAQ;IACvB,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,GAAG,CAAC;CACR;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,QAAQ,GAAG,MAAM,CAQ3C;AAiBD;;;;;;;;;GASG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAIxC;AAED;;;;;;;GAOG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAU3C;AAED;;;;;;;GAOG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAExC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function r(t){return!(t.length%2===1||t.match(/^[0-9a-f]+$/)==null)}function u(t){switch(t.t){case"int":return i(t.v);case"seq":return l(t.v)}return""}function i(t){return typeof t=="string"&&r(t)?`02${s(t.length/2)}${t}`:""}function l(t){let e="";for(let n=0;n<t.length;n++)e+=u(t[n]);return`30${s(e.length/2)}${e}`}function h(t){return t=t.replace(/^(00){1,}/,""),t.match(/^[8-9a-f]/)?"00"+t:t}function s(t){let e;return t<128?(e=o(t.toString(16)),e):(e=o(t.toString(16)),(128+e.length/2).toString(16)+e)}function o(t){return t.length%2===1?`0${t}`:t}export{u as getASN1,s as getLength,o as hexpad,h as pospad};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var s=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var $=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var l=(t,e)=>()=>(t&&(e=t(t=0)),e);var b=(t,e)=>{for(var n in e)s(t,n,{get:e[n],enumerable:!0})},d=(t,e,n,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of $(e))!m.call(t,r)&&r!==n&&s(t,r,{get:()=>e[r],enumerable:!(o=p(e,r))||o.enumerable});return t};var x=t=>d(s({},"__esModule",{value:!0}),t);function a(t){return!(t.length%2===1||t.match(/^[0-9a-f]+$/)==null)}var f=l(()=>{});var h={};b(h,{getASN1:()=>c,getLength:()=>i,hexpad:()=>u,pospad:()=>I});function c(t){switch(t.t){case"int":return w(t.v);case"seq":return S(t.v)}return""}function w(t){return typeof t=="string"&&a(t)?`02${i(t.length/2)}${t}`:""}function S(t){let e="";for(let n=0;n<t.length;n++)e+=c(t[n]);return`30${i(e.length/2)}${e}`}function I(t){return t=t.replace(/^(00){1,}/,""),t.match(/^[8-9a-f]/)?"00"+t:t}function i(t){let e;return t<128?(e=u(t.toString(16)),e):(e=u(t.toString(16)),(128+e.length/2).toString(16)+e)}function u(t){return t.length%2===1?`0${t}`:t}var g=l(()=>{"use strict";f()});module.exports=(g(),x(h));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../src/index.cts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "typepki-asn1gen",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"author": "Kenji Urushima <kenji.urushima@gmail.com>",
|
|
5
|
+
"description": "ASN.1 data generator for TypePKI library (beta)",
|
|
6
|
+
"homepage": "https://kjur.github.io/typepki-asn1gen",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/kjur/typepki-asn1gen.git"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/kjur/typepki-asn1gen/issues"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"ASN.1"
|
|
16
|
+
],
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"scripts": {
|
|
19
|
+
"deploy": "",
|
|
20
|
+
"build": "run-z build:transpile,build:esm,build:cjs",
|
|
21
|
+
"build:transpile": "bun build.mts",
|
|
22
|
+
"build:esm": "tsc --project tsconfig.build.esm.json",
|
|
23
|
+
"build:cjs": "tsc --project tsconfig.build.cjs.json",
|
|
24
|
+
"check": "run-z check:type,check:static",
|
|
25
|
+
"check:type": "tsc --noEmit",
|
|
26
|
+
"check:static": "biome check .",
|
|
27
|
+
"fix": "$npm_execpath run check:static --apply-unsafe",
|
|
28
|
+
"test": "bun test",
|
|
29
|
+
"precommit": "run-z check:type fix test build",
|
|
30
|
+
"prepublish": "attw --pack .",
|
|
31
|
+
"run-z": "run-z",
|
|
32
|
+
"doc": "typedoc --includeVersion --cleanOutputDir false --tsconfig ./tsconfig.typedoc.json --options ./typedoc.json src/index.mts"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"src",
|
|
36
|
+
"dist",
|
|
37
|
+
"package.json",
|
|
38
|
+
"tsconfig.json",
|
|
39
|
+
"LICENSE"
|
|
40
|
+
],
|
|
41
|
+
"exports": {
|
|
42
|
+
".": {
|
|
43
|
+
"import": {
|
|
44
|
+
"types": "./dist/import/index.d.mts",
|
|
45
|
+
"default": "./dist/import/index.mjs"
|
|
46
|
+
},
|
|
47
|
+
"require": {
|
|
48
|
+
"types": "./dist/require/index.d.cts",
|
|
49
|
+
"default": "./dist/require/index.cjs"
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@arethetypeswrong/cli": "^0.15.0",
|
|
55
|
+
"@biomejs/biome": "1.5.3",
|
|
56
|
+
"@changesets/cli": "^2.27.1",
|
|
57
|
+
"@types/bun": "^1.1.1",
|
|
58
|
+
"bun-types": "^1.1.4",
|
|
59
|
+
"esbuild": "^0.20.0",
|
|
60
|
+
"jest": "^29.7.0",
|
|
61
|
+
"run-z": "^2.0.0",
|
|
62
|
+
"typedoc": "^0.25.13",
|
|
63
|
+
"typescript": "^5.4.5"
|
|
64
|
+
},
|
|
65
|
+
"dependencies": {
|
|
66
|
+
"typepki-strconv": "^0.3.0"
|
|
67
|
+
}
|
|
68
|
+
}
|
package/src/index.cts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require("./index.mts");
|
package/src/index.mts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { ishex } from "typepki-strconv";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* ASN.1 data object definition
|
|
5
|
+
* @example
|
|
6
|
+
* let d: ASN1Data = { t: "int", v: "1234" };
|
|
7
|
+
*/
|
|
8
|
+
export interface ASN1Data {
|
|
9
|
+
t: string;
|
|
10
|
+
v: any;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* generate ASN.1 DER/BER encoded hexadecimal string from JSON data
|
|
15
|
+
* @param p - JSON object representing ASN.1 structure
|
|
16
|
+
* @return hexadecimal string of ASN.1 DER/BER encoded hexadecimal string
|
|
17
|
+
* @example
|
|
18
|
+
* getASN1({ t: "int", v: "12ab" }) -> "020212ab"
|
|
19
|
+
* getASN1({ t: "seq", v: [
|
|
20
|
+
* { t: "int", v: "01" },
|
|
21
|
+
* { t: "int", v: "02" }
|
|
22
|
+
* ]}) -> "3006020101020102"
|
|
23
|
+
*/
|
|
24
|
+
export function getASN1(p: ASN1Data): string {
|
|
25
|
+
switch (p.t) {
|
|
26
|
+
case "int":
|
|
27
|
+
return generateASN1_int(p.v);
|
|
28
|
+
case "seq":
|
|
29
|
+
return generateASN1_seq(p.v);
|
|
30
|
+
}
|
|
31
|
+
return "";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function generateASN1_int(v: string | object): string {
|
|
35
|
+
if (typeof v == "string" && ishex(v)) {
|
|
36
|
+
return `02${getLength(v.length / 2)}${v}`;
|
|
37
|
+
}
|
|
38
|
+
return "";
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function generateASN1_seq(v: Array<ASN1Data>): string {
|
|
42
|
+
let hV = "";
|
|
43
|
+
for (let i = 0; i < v.length; i++) {
|
|
44
|
+
hV += getASN1(v[i]);
|
|
45
|
+
}
|
|
46
|
+
return `30${getLength(hV.length / 2)}${hV}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* convert hexadecimal positive integer to ASN.1 integer value
|
|
51
|
+
* @param h - hexadecimal string of positive integer
|
|
52
|
+
* @return ASN.1 Integer DER encoded hexadecimal string
|
|
53
|
+
* @example
|
|
54
|
+
* pospad("01") -> "01"
|
|
55
|
+
* pospad("ab") -> "00ab"
|
|
56
|
+
* pospad("00000012") -> "12"
|
|
57
|
+
* pospad("000000ab") -> "00ab"
|
|
58
|
+
*/
|
|
59
|
+
export function pospad(h: string): string {
|
|
60
|
+
h = h.replace(/^(00){1,}/, '');
|
|
61
|
+
if (h.match(/^[8-9a-f]/)) return "00" + h;
|
|
62
|
+
return h;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* get ASN.1 TLV length octet(s) by TLV value octet length
|
|
67
|
+
* @param n - TLV value octet length
|
|
68
|
+
* @return hexadecimal string of TLV length octets
|
|
69
|
+
* @example
|
|
70
|
+
* getLength(3) -> "03"
|
|
71
|
+
* getLength(128) -> "8180"
|
|
72
|
+
*/
|
|
73
|
+
export function getLength(n: number): string {
|
|
74
|
+
let hL;
|
|
75
|
+
if (n < 128) {
|
|
76
|
+
hL = hexpad(n.toString(16));
|
|
77
|
+
return hL;
|
|
78
|
+
}
|
|
79
|
+
hL = hexpad(n.toString(16));
|
|
80
|
+
const iTop = 128 + (hL.length / 2);
|
|
81
|
+
const hTop = iTop.toString(16);
|
|
82
|
+
return hTop + hL;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* zero padding for hexadecimal string
|
|
87
|
+
* @param s - odd or even length hexadecimal string
|
|
88
|
+
* @return even length zero padded hexadecimal string
|
|
89
|
+
* @example
|
|
90
|
+
* hexpad("1") -> "01"
|
|
91
|
+
* hexpad("ab3c") -> "ab3c"
|
|
92
|
+
*/
|
|
93
|
+
export function hexpad(s: string): string {
|
|
94
|
+
return (s.length % 2 === 1) ? `0${s}` : s;
|
|
95
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { pospad, getLength, getASN1 } from "./index.mts";
|
|
3
|
+
|
|
4
|
+
test("pospad", () => {
|
|
5
|
+
expect(pospad("1234")).toBe("1234");
|
|
6
|
+
expect(pospad("001234")).toBe("1234");
|
|
7
|
+
expect(pospad("00001234")).toBe("1234");
|
|
8
|
+
expect(pospad("0000abcd")).toBe("00abcd");
|
|
9
|
+
expect(pospad("abcd")).toBe("00abcd");
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test("getLength", () => {
|
|
13
|
+
expect(getLength(1)).toBe("01");
|
|
14
|
+
expect(getLength(127)).toBe("7f");
|
|
15
|
+
expect(getLength(128)).toBe("8180");
|
|
16
|
+
expect(getLength(732)).toBe("8202dc");
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test("getASN1", () => {
|
|
20
|
+
expect(getASN1({t: "int", v: "12ab"})).toBe("020212ab");
|
|
21
|
+
expect(getASN1({t: "seq", v: [
|
|
22
|
+
{t: "int", v: "01"},
|
|
23
|
+
{t: "int", v: "02"}
|
|
24
|
+
]})).toBe("3006020101020102");
|
|
25
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"types": ["bun-types"],
|
|
4
|
+
|
|
5
|
+
"lib": ["esnext"],
|
|
6
|
+
"module": "esnext",
|
|
7
|
+
"target": "esnext",
|
|
8
|
+
"moduleResolution": "bundler",
|
|
9
|
+
|
|
10
|
+
"noEmit": true,
|
|
11
|
+
|
|
12
|
+
"allowImportingTsExtensions": true,
|
|
13
|
+
"moduleDetection": "force",
|
|
14
|
+
|
|
15
|
+
"strict": true,
|
|
16
|
+
"forceConsistentCasingInFileNames": true,
|
|
17
|
+
"skipLibCheck": true,
|
|
18
|
+
|
|
19
|
+
},
|
|
20
|
+
"exclude": ["_work"]
|
|
21
|
+
}
|