typepki-strconv 0.10.0 → 0.11.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/dist/import/conv_pem.d.mts +18 -2
- package/dist/import/conv_pem.d.mts.map +1 -1
- package/dist/import/index.mjs +3 -3
- package/dist/require/index.cjs +3 -3
- package/package.json +1 -1
- package/src/conv_pem.mts +36 -8
- package/src/conv_pem.test.mts +36 -0
- package/src/index.mts +1 -1
|
@@ -40,15 +40,31 @@ export declare function pemtohex(s: string, sHead?: string): string;
|
|
|
40
40
|
* get Base64 string from PEM format data
|
|
41
41
|
* @param pem - PEM formatted string
|
|
42
42
|
* @param sHead - PEM header string without BEGIN/END
|
|
43
|
-
* @return
|
|
43
|
+
* @return base64 string in the first PEM
|
|
44
44
|
*
|
|
45
45
|
* @description
|
|
46
46
|
* This static method gets a Base64 string of contents
|
|
47
|
-
* from PEM format data.
|
|
47
|
+
* from PEM format data. As for the "pem", some garbled text
|
|
48
|
+
* before and after the PEM header also be acceptable.
|
|
49
|
+
* When two or more PEMs are included, the first one will be
|
|
50
|
+
* returned.
|
|
48
51
|
*
|
|
49
52
|
* @example
|
|
50
53
|
* pemtob64("-----BEGIN CERTIFICATE...", "CERTIFICATE") -> "MIIBvTCC..."
|
|
54
|
+
* pemtob64("-----BEGIN CERTIFICATE...", "CERTIFICATE") -> "MIIBvTCC..."
|
|
51
55
|
* pemtob64("-----BEGIN CERTIFICATE...") -> "MIIBvTCC..."
|
|
56
|
+
* pem2 = `
|
|
57
|
+
* junk-text
|
|
58
|
+
* -----BEGIN FOO----
|
|
59
|
+
* YWFh
|
|
60
|
+
* -----BEGIN FOO----
|
|
61
|
+
* junk-text
|
|
62
|
+
* -----BEGIN BOO BOO----
|
|
63
|
+
* enp6
|
|
64
|
+
* -----BEGIN BOO BOO----
|
|
65
|
+
* junk-text
|
|
66
|
+
* `
|
|
67
|
+
* pemtob64(pem2) -> "YWFh"
|
|
52
68
|
*/
|
|
53
69
|
export declare function pemtob64(pem: string, sHead?: string): string;
|
|
54
70
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"conv_pem.d.mts","sourceRoot":"","sources":["../../src/conv_pem.mts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,QAAQ,CACtB,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,MAAM,EACjB,SAAS,SAAS,GACjB,MAAM,
|
|
1
|
+
{"version":3,"file":"conv_pem.d.mts","sourceRoot":"","sources":["../../src/conv_pem.mts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,QAAQ,CACtB,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,MAAM,EACjB,SAAS,SAAS,GACjB,MAAM,CAMR;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAE1D;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAuB5D;AAID;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,QAAQ,CACtB,GAAG,EAAE,MAAM,EACX,SAAS,EAAE,MAAM,EACjB,SAAS,SAAS,GACjB,MAAM,CAMR;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAI5C;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,SAAS,SAAS,GAAG,MAAM,CAKzE"}
|
package/dist/import/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
function x(r){return S(
|
|
2
|
-
`){return`-----BEGIN ${t}-----${e}${y(m(r),64)}${e}-----END ${t}-----${e}`}function st(r,t){return a(v(r,t))}function v(r,t){let e=r;if(e.indexOf(
|
|
1
|
+
function x(r){return S(E(r))}function S(r){let t="";for(let e=0;e<r.length;e++){let n=r[e].toString(16);n.length===1&&(n=`0${n}`),t=t+n}return t}function Y(r){let t=[];for(let e=0;e<r.length;e+=2)t.push(parseInt(r.slice(e,e+2),16));return t}function E(r){let t=new Array;for(let e=0;e<r.length;e++)t[e]=r.charCodeAt(e);return t}function j(r){let t="";for(let e=0;e<r.length;e++)t=t+String.fromCharCode(r[e]);return t}function R(r){return r.replace(/%/g,"")}function b(r){return r.replace(/(..)/g,"%$1")}function k(r){let t=encodeURIComponent(r),e="";for(let n=0;n<t.length;n++)t[n]==="%"?(e=e+t.substr(n,3),n=n+2):e=`${e}%${x(t[n])}`;return e}function a(r){return Buffer.from(r,"base64").toString("hex")}function m(r){return Buffer.from(r,"hex").toString("base64")}function g(r){try{return decodeURIComponent(b(r))}catch{throw new Error("malformed hexadecimal UTF-8 string")}}function $(r){return Buffer.from(r,"utf8").toString("hex")}function N(r){return Buffer.from(r,"base64").toString("utf8")}function F(r){return Buffer.from(r,"utf8").toString("base64")}function O(r){return Buffer.from(p(r),"base64").toString("utf8")}function L(r){return d(Buffer.from(r,"utf8").toString("base64"))}function V(r){return a(p(r))}function P(r){return d(m(r))}function _(r){let t="";for(let e=0;e<r.length;e+=2){let n=parseInt(r.substr(e,2),16);t+=String.fromCharCode(n)}return t}function G(r){let t="";for(let e=0;e<r.length;e++)t+=`0${r.charCodeAt(e).toString(16)}`.slice(-2);return t}function H(r){return Buffer.from(r).toString("base64")}function d(r){let t=r;return t=t.replace(/\=/g,""),t=t.replace(/\+/g,"-"),t=t.replace(/\//g,"_"),t}function p(r){let t=r;return t.length%4===2?t=`${t}==`:t.length%4===3&&(t=`${t}=`),t=t.replace(/-/g,"+"),t=t.replace(/_/g,"/"),t}var K=r=>{if(r.match(/^[01]+$/)==null)return null;try{let t=parseInt(r,2);return U(t)}catch{return null}};function q(r){if(typeof r!="string"||r.length%2!==0||!r.match(/^[0-9a-f]+$/))return null;try{let t=parseInt(r.substr(0,2),16);if(t<0||7<t)return null;let e=r.substr(2),n="";for(let i=0;i<e.length;i+=2){let o=e.substr(i,2),s=parseInt(o,16).toString(2);s=`0000000${s}`.slice(-8),n+=s}return n.substr(0,n.length-t)}catch{return null}}function U(r){if(typeof r!="number"||r<0)return null;let t=Number(r).toString(2),e=8-t.length%8;e===8&&(e=0),t=t+T("",e,"0");let n=parseInt(t,2).toString(16);return n.length%2===1&&(n=`0${n}`),`0${e}`+n}var T=(r,t,e)=>{let n=e===void 0?"0":e;return r.length>=t?r:new Array(t-r.length+1).join(n)+r};function J(r,t){let e=0;for(let o=0;o<r.length;o++)e|=1<<t[r[o]];let n=e.toString(2),i="";for(let o=n.length-1;o>=0;o--)i+=n[o];return i}function Q(r,t){let n=Object.keys(t).map(s=>t[s]),i={};Object.keys(t).map(s=>{i[t[s]]=s});let o=[];return r.split("").map((s,l)=>{s==="1"&&o.push(i[l.toString()])}),o}function W(r,t,e){let n=r,i=t.split(".");for(let o=0;o<i.length&&n;o++){let s=i[o];if(typeof n=="object"&&"length"in n&&s.match(/^[0-9]+$/))n=n[parseInt(s)];else if(typeof n=="object"&&s in n)n=n[s];else return e}return n||n===!1?n:e}function X(r){return!(r.length%2===1||r.match(/^[0-9a-f]+$/)==null)}function D(r){let t=r.replace(/\s+/g,"");return!!(t.match(/^[0-9A-Za-z+\/]+={0,3}$/)&&t.length%4===0)}function tt(r){if(r.match(/[+/=]/))return!1;let t=p(r);return D(t)}function rt(r){return g(nt(r))}function et(r){return ot($(r))}function nt(r){let t=r.match(/.{1,2}/g);if(t==null)return"";let e=[];for(let n=0;n<t.length;n++){let i=parseInt(t[n],16);161<=i&&i<=191?(e.push("c2"),e.push(t[n])):192<=i&&i<=255?(e.push("c3"),e.push((i-64).toString(16))):e.push(t[n])}return e.join("")}function ot(r){let t=r.match(/.{1,2}/g);if(t==null)return"";let e=[];for(let n=0;n<t.length;n++)if(t[n]==="c2")n++,e.push(t[n]);else if(t[n]==="c3"){n++;let i=parseInt(t[n],16)+64;e.push(i.toString(16))}else e.push(t[n]);return e.join("")}function it(r,t,e=`\r
|
|
2
|
+
`){return`-----BEGIN ${t}-----${e}${y(m(r),64,e)}${e}-----END ${t}-----${e}`}function st(r,t){return a(v(r,t))}function v(r,t){let e=r;if(t!==void 0){let n=`-----BEGIN ${t}-----`,i=`-----END ${t}-----`,o=e.indexOf(n);if(o==-1)throw new Error("can't find PEM header");return e=e.slice(o+n.length),o=e.indexOf(i),e=e.slice(0,o),e.replace(/\s+/g,"")}else{let n="-----BEGIN ",i="-----END ",o=e.indexOf(n);if(o==-1)throw new Error("can't find PEM header");return e=e.slice(o+n.length),e=e.replace(/^[0-9A-Za-z ]+-----/s,""),o=e.indexOf(i),e=e.slice(0,o),e.replace(/\s+/g,"")}}function ut(r,t,e=`\r
|
|
3
3
|
`){return`-----BEGIN ${t}-----${e}${y(r,64,e)}${e}-----END ${t}-----${e}`}function lt(r){let t=r.replace(/[^0-9A-Za-z\/+=]*/g,"");return a(t)}function y(r,t,e=`\r
|
|
4
|
-
`){let n=r;return n=n.replace(new RegExp(`(.{${t}})`,"g"),`$1${e}`),n=n.replace(/\s+$/,""),n}function
|
|
4
|
+
`){let n=r;return n=n.replace(new RegExp(`(.{${t}})`,"g"),`$1${e}`),n=n.replace(/\s+$/,""),n}function ct(r){if(r.length%2!==0)throw"input is not even length";if(r.match(/^[0-9A-Fa-f]+$/)==null)throw"input is not hexadecimal";let t=new ArrayBuffer(r.length/2),e=new DataView(t);for(let n=0;n<r.length/2;n++)e.setUint8(n,parseInt(r.substr(n*2,2),16));return t}function ft(r){let t="",e=new DataView(r);for(let n=0;n<r.byteLength;n++)t+=`00${e.getUint8(n).toString(16)}`.slice(-2);return t}function w(r){let t=r,e,n,i,o,s,l,u,f,h;t=M(t);let c=t.match(/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(|\.\d+)Z$/);if(c)return e=parseInt(c[1]),n=parseInt(c[2])-1,i=parseInt(c[3]),o=parseInt(c[4]),s=parseInt(c[5]),l=parseInt(c[6]),u=0,f=c[7],f!==""&&(h=`${f.substr(1)}00`.substr(0,3),u=parseInt(h)),Date.UTC(e,n,i,o,s,l,u);throw new Error(`unsupported zulu format: ${t}`)}function gt(r){let t=new Date(r),e=`0000${t.getUTCFullYear()}`.slice(-4),n=`00${t.getUTCMonth()+1}`.slice(-2),i=`00${t.getUTCDate()}`.slice(-2),o=`00${t.getUTCHours()}`.slice(-2),s=`00${t.getUTCMinutes()}`.slice(-2),l=`00${t.getUTCSeconds()}`.slice(-2),u=`000${t.getUTCMilliseconds()}`.slice(-3);return u=u.replace(/0+$/,""),u=u!==""?`.${u}`:u,`${e+n+i+o+s+l+u}Z`}function at(r){return Math.round(w(r)/1e3)}function pt(r){return new Date(w(r))}function ht(r,t=!1,e=!1){let n,i=r.getUTCFullYear();if(t){if(i<1950||2049<i)throw new Error(`not proper year for UTCTime: ${i}`);n=`${i}`.slice(-2)}else n=`000${i}`.slice(-4);if(n+=`0${r.getUTCMonth()+1}`.slice(-2),n+=`0${r.getUTCDate()}`.slice(-2),n+=`0${r.getUTCHours()}`.slice(-2),n+=`0${r.getUTCMinutes()}`.slice(-2),n+=`0${r.getUTCSeconds()}`.slice(-2),e){let o=r.getUTCMilliseconds();if(o!==0){let s=`00${o}`.slice(-3);s=s.replace(/0+$/g,""),n+=`.${s}`}}return n+="Z",n}function M(r){return r.match(/^[0-9]{12}Z$/)||r.match(/^[0-9]{12}[.][0-9]*Z$/)?r.match(/^[0-4]/)?`20${r}`:`19${r}`:r}function mt(r){let t=r.match(/^(\d{2,4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})Z$/);if(!t)throw new Error("Invalid format: expected YYYYMMDDHHmmssZ or YYYYMMDDHHmmssZ");let[,e,n,i,o,s,l]=t,u=e;return u.length===2&&(parseInt(u.slice(0,1))<5?u=`20${u}`:u=`19${u}`),`${u}-${n}-${i}T${o}:${s}:${l}Z`}function xt(r,t=!1){let e=r.match(/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z$/);if(!e)throw new Error("Invalid format: expected YYYY-MM-DDTHH:mm:ssZ");let[,n,i,o,s,l,u]=e,f=parseInt(n);if(t)if(1950<=f&&f<=2049)n=n.slice(2,4);else throw new Error(`Invalid year for UTCTime: ${n}`);return`${n}${i}${o}${s}${l}${u}Z`}function I(r){let t=r,e="malformed IPv6 address";if(!t.match(/^[0-9A-Fa-f:]+$/))throw e;t=t.toLowerCase();let n=t.split(":").length-1;if(n<2)throw e;let i=":".repeat(7-n+2);t=t.replace("::",i);let o=t.split(":");if(o.length!==8)throw e;for(let s=0;s<8;s++)o[s]=`0000${o[s]}`.slice(-4);return o.join("")}function A(r){let t=r;if(!t.match(/^[0-9A-Fa-f]{32}$/))throw new Error(`malformed IPv6 address: ${t}`);t=t.toLowerCase();let e=t.match(/.{1,4}/g)||[];e=e.map(o=>o.replace(/^0+/,"")),e=e.map(o=>o===""?"0":o),t=`:${e.join(":")}:`;let n=t.match(/:(0:){2,}/g);if(n==null)return t.slice(1,-1);let i=n.sort().slice(-1)[0];return t=t.replace(i.substr(0,i.length-1),":"),t.substr(0,2)!=="::"&&(t=t.substr(1)),t.substr(-2,2)!=="::"&&(t=t.substr(0,t.length-1)),t}function z(r){let t=new Error("malformed hex value");if(!r.match(/^([0-9A-Fa-f][0-9A-Fa-f]){1,}$/))throw t;if(r.length===8){let e;try{return e=`${parseInt(r.substr(0,2),16)}.${parseInt(r.substr(2,2),16)}.${parseInt(r.substr(4,2),16)}.${parseInt(r.substr(6,2),16)}`,e}catch{throw t}}else if(r.length===16)try{return`${z(r.substr(0,8))}/${B(r.substr(8))}`}catch{throw t}else{if(r.length===32)return A(r);if(r.length===64)try{return`${A(r.substr(0,32))}/${B(r.substr(32))}`}catch{throw t}else return r}}function Z(r){let t=r,e=new Error("malformed IP address");if(t=t.toLowerCase(),!t.match(/^[0-9a-f.:/]+$/))throw e;if(t.match(/^[0-9.]+$/)){let n=t.split(".");if(n.length!==4)throw e;let i="";try{for(let o=0;o<4;o++){let s=parseInt(n[o]);i+=`0${s.toString(16)}`.slice(-2)}return i}catch{throw e}}else if(t.match(/^[0-9.]+\/[0-9]+$/)){let n=t.split("/");return Z(n[0])+C(parseInt(n[1]),32)}else{if(t.match(/^[0-9a-f:]+$/)&&t.indexOf(":")!==-1)return I(t);if(t.match(/^[0-9a-f:]+\/[0-9]+$/)&&t.indexOf(":")!==-1){let n=t.split("/");return I(n[0])+C(parseInt(n[1]),128)}else throw e}}function B(r){let t=new Error("malformed mask"),e;try{e=parseInt(r,16).toString(2)}catch{throw t}if(!e.match(/^1*0*$/))throw t;return e.replace(/0+$/,"").length}function C(r,t){if(t===32&&r===0)return"00000000";if(t===128&&r===0)return"00000000000000000000000000000000";let e=Array(r+1).join("1")+Array(t-r+1).join("0");return parseInt(e,2).toString(16)}function bt(r){function t(i){let o=parseInt(i.substr(0,2),16),s=parseInt(i.substr(2),16);if(o===0&&s<128)return String.fromCharCode(s);if(o<8){let h=192|(o&7)<<3|(s&192)>>6,c=128|s&63;return g(h.toString(16)+c.toString(16))}let l=224|(o&240)>>4,u=128|(o&15)<<2|(s&192)>>6,f=128|s&63;return g(l.toString(16)+u.toString(16)+f.toString(16))}let e=r.match(/.{4}/g);return e==null?"":e.map(t).join("")}function $t(r){return r.length%2===1?`0${r}`:r}var jt="typepki-strconv 0.11.0 kjur.github.io/typepki-strconv";export{ft as ArrayBuffertohex,S as BAtohex,j as BAtos,jt as VERSION,W as aryval,lt as b64nltohex,d as b64tob64u,a as b64tohex,ut as b64topem,N as b64toutf8,p as b64utob64,V as b64utohex,O as b64utoutf8,K as binstrtobitstr,Q as binstrtonamearray,q as bitstrtobinstr,ht as datetozulu,k as encodeURIComponentAll,y as foldnl,$t as hexpad,ct as hextoArrayBuffer,Y as hextoBA,m as hextob64,P as hextob64u,z as hextoip,A as hextoipv6,it as hextopem,_ as hextorstr,b as hextouricmp,g as hextoutf8,U as inttobitstr,C as ipnetmask,B as ipprefixlen,Z as iptohex,I as ipv6tohex,D as isBase64,tt as isBase64URL,X as ishex,xt as iso8601tozulu,rt as iso88591hextoutf8,gt as msectozulu,J as namearraytobinstr,v as pemtob64,st as pemtohex,H as rstrtob64,G as rstrtohex,E as stoBA,x as stohex,T as strpad,M as timetogen,bt as ucs2hextoutf8,R as uricmptohex,F as utf8tob64,L as utf8tob64u,$ as utf8tohex,et as utf8toiso88591hex,pt as zulutodate,mt as zulutoiso8601,w as zulutomsec,at as zulutosec};
|
package/dist/require/index.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
`){return`-----BEGIN ${t}-----${e}${I(x(r),64)}${e}-----END ${t}-----${e}`}function gt(r,t){return p(
|
|
1
|
+
"use strict";var U=Object.defineProperty;var Mt=Object.getOwnPropertyDescriptor;var zt=Object.getOwnPropertyNames;var Zt=Object.prototype.hasOwnProperty;var c=(r,t)=>()=>(r&&(t=r(r=0)),t);var Yt=(r,t)=>{for(var e in t)U(r,e,{get:t[e],enumerable:!0})},jt=(r,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of zt(t))!Zt.call(r,i)&&i!==e&&U(r,i,{get:()=>t[i],enumerable:!(n=Mt(t,i))||n.enumerable});return r};var Rt=r=>jt(U({},"__esModule",{value:!0}),r);function $(r){return T(D(r))}function T(r){let t="";for(let e=0;e<r.length;e++){let n=r[e].toString(16);n.length===1&&(n=`0${n}`),t=t+n}return t}function F(r){let t=[];for(let e=0;e<r.length;e+=2)t.push(parseInt(r.slice(e,e+2),16));return t}function D(r){let t=new Array;for(let e=0;e<r.length;e++)t[e]=r.charCodeAt(e);return t}function O(r){let t="";for(let e=0;e<r.length;e++)t=t+String.fromCharCode(r[e]);return t}var v=c(()=>{"use strict"});function L(r){return r.replace(/%/g,"")}function d(r){return r.replace(/(..)/g,"%$1")}function V(r){let t=encodeURIComponent(r),e="";for(let n=0;n<t.length;n++)t[n]==="%"?(e=e+t.substr(n,3),n=n+2):e=`${e}%${$(t[n])}`;return e}var M=c(()=>{"use strict";v()});function p(r){return Buffer.from(r,"base64").toString("hex")}function x(r){return Buffer.from(r,"hex").toString("base64")}function a(r){try{return decodeURIComponent(d(r))}catch{throw new Error("malformed hexadecimal UTF-8 string")}}function y(r){return Buffer.from(r,"utf8").toString("hex")}function P(r){return Buffer.from(r,"base64").toString("utf8")}function _(r){return Buffer.from(r,"utf8").toString("base64")}function G(r){return Buffer.from(h(r),"base64").toString("utf8")}function H(r){return w(Buffer.from(r,"utf8").toString("base64"))}function K(r){return p(h(r))}function q(r){return w(x(r))}function J(r){let t="";for(let e=0;e<r.length;e+=2){let n=parseInt(r.substr(e,2),16);t+=String.fromCharCode(n)}return t}function Q(r){let t="";for(let e=0;e<r.length;e++)t+=`0${r.charCodeAt(e).toString(16)}`.slice(-2);return t}function W(r){return Buffer.from(r).toString("base64")}function w(r){let t=r;return t=t.replace(/\=/g,""),t=t.replace(/\+/g,"-"),t=t.replace(/\//g,"_"),t}function h(r){let t=r;return t.length%4===2?t=`${t}==`:t.length%4===3&&(t=`${t}=`),t=t.replace(/-/g,"+"),t=t.replace(/_/g,"/"),t}function tt(r){if(typeof r!="string"||r.length%2!==0||!r.match(/^[0-9a-f]+$/))return null;try{let t=parseInt(r.substr(0,2),16);if(t<0||7<t)return null;let e=r.substr(2),n="";for(let i=0;i<e.length;i+=2){let o=e.substr(i,2),s=parseInt(o,16).toString(2);s=`0000000${s}`.slice(-8),n+=s}return n.substr(0,n.length-t)}catch{return null}}function z(r){if(typeof r!="number"||r<0)return null;let t=Number(r).toString(2),e=8-t.length%8;e===8&&(e=0),t=t+Z("",e,"0");let n=parseInt(t,2).toString(16);return n.length%2===1&&(n=`0${n}`),`0${e}`+n}function rt(r,t){let e=0;for(let o=0;o<r.length;o++)e|=1<<t[r[o]];let n=e.toString(2),i="";for(let o=n.length-1;o>=0;o--)i+=n[o];return i}function et(r,t){let n=Object.keys(t).map(s=>t[s]),i={};Object.keys(t).map(s=>{i[t[s]]=s});let o=[];return r.split("").map((s,l)=>{s==="1"&&o.push(i[l.toString()])}),o}function nt(r,t,e){let n=r,i=t.split(".");for(let o=0;o<i.length&&n;o++){let s=i[o];if(typeof n=="object"&&"length"in n&&s.match(/^[0-9]+$/))n=n[parseInt(s)];else if(typeof n=="object"&&s in n)n=n[s];else return e}return n||n===!1?n:e}var X,Z,m=c(()=>{"use strict";M();X=r=>{if(r.match(/^[01]+$/)==null)return null;try{let t=parseInt(r,2);return z(t)}catch{return null}};Z=(r,t,e)=>{let n=e===void 0?"0":e;return r.length>=t?r:new Array(t-r.length+1).join(n)+r}});function ot(r){return!(r.length%2===1||r.match(/^[0-9a-f]+$/)==null)}function Y(r){let t=r.replace(/\s+/g,"");return!!(t.match(/^[0-9A-Za-z+\/]+={0,3}$/)&&t.length%4===0)}function it(r){if(r.match(/[+/=]/))return!1;let t=h(r);return Y(t)}var st=c(()=>{"use strict";m()});function ut(r){return a(kt(r))}function lt(r){return Nt(y(r))}function kt(r){let t=r.match(/.{1,2}/g);if(t==null)return"";let e=[];for(let n=0;n<t.length;n++){let i=parseInt(t[n],16);161<=i&&i<=191?(e.push("c2"),e.push(t[n])):192<=i&&i<=255?(e.push("c3"),e.push((i-64).toString(16))):e.push(t[n])}return e.join("")}function Nt(r){let t=r.match(/.{1,2}/g);if(t==null)return"";let e=[];for(let n=0;n<t.length;n++)if(t[n]==="c2")n++,e.push(t[n]);else if(t[n]==="c3"){n++;let i=parseInt(t[n],16)+64;e.push(i.toString(16))}else e.push(t[n]);return e.join("")}var ct=c(()=>{"use strict";m()});function ft(r,t,e=`\r
|
|
2
|
+
`){return`-----BEGIN ${t}-----${e}${I(x(r),64,e)}${e}-----END ${t}-----${e}`}function gt(r,t){return p(j(r,t))}function j(r,t){let e=r;if(t!==void 0){let n=`-----BEGIN ${t}-----`,i=`-----END ${t}-----`,o=e.indexOf(n);if(o==-1)throw new Error("can't find PEM header");return e=e.slice(o+n.length),o=e.indexOf(i),e=e.slice(0,o),e.replace(/\s+/g,"")}else{let n="-----BEGIN ",i="-----END ",o=e.indexOf(n);if(o==-1)throw new Error("can't find PEM header");return e=e.slice(o+n.length),e=e.replace(/^[0-9A-Za-z ]+-----/s,""),o=e.indexOf(i),e=e.slice(0,o),e.replace(/\s+/g,"")}}function at(r,t,e=`\r
|
|
3
3
|
`){return`-----BEGIN ${t}-----${e}${I(r,64,e)}${e}-----END ${t}-----${e}`}function pt(r){let t=r.replace(/[^0-9A-Za-z\/+=]*/g,"");return p(t)}function I(r,t,e=`\r
|
|
4
|
-
`){let n=r;return n=n.replace(new RegExp(`(.{${t}})`,"g"),`$1${e}`),n=n.replace(/\s+$/,""),n}var ht=
|
|
4
|
+
`){let n=r;return n=n.replace(new RegExp(`(.{${t}})`,"g"),`$1${e}`),n=n.replace(/\s+$/,""),n}var ht=c(()=>{"use strict";m()});function mt(r){if(r.length%2!==0)throw"input is not even length";if(r.match(/^[0-9A-Fa-f]+$/)==null)throw"input is not hexadecimal";let t=new ArrayBuffer(r.length/2),e=new DataView(t);for(let n=0;n<r.length/2;n++)e.setUint8(n,parseInt(r.substr(n*2,2),16));return t}function xt(r){let t="",e=new DataView(r);for(let n=0;n<r.byteLength;n++)t+=`00${e.getUint8(n).toString(16)}`.slice(-2);return t}var bt=c(()=>{"use strict"});function A(r){let t=r,e,n,i,o,s,l,u,g,b;t=R(t);let f=t.match(/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(|\.\d+)Z$/);if(f)return e=parseInt(f[1]),n=parseInt(f[2])-1,i=parseInt(f[3]),o=parseInt(f[4]),s=parseInt(f[5]),l=parseInt(f[6]),u=0,g=f[7],g!==""&&(b=`${g.substr(1)}00`.substr(0,3),u=parseInt(b)),Date.UTC(e,n,i,o,s,l,u);throw new Error(`unsupported zulu format: ${t}`)}function $t(r){let t=new Date(r),e=`0000${t.getUTCFullYear()}`.slice(-4),n=`00${t.getUTCMonth()+1}`.slice(-2),i=`00${t.getUTCDate()}`.slice(-2),o=`00${t.getUTCHours()}`.slice(-2),s=`00${t.getUTCMinutes()}`.slice(-2),l=`00${t.getUTCSeconds()}`.slice(-2),u=`000${t.getUTCMilliseconds()}`.slice(-3);return u=u.replace(/0+$/,""),u=u!==""?`.${u}`:u,`${e+n+i+o+s+l+u}Z`}function dt(r){return Math.round(A(r)/1e3)}function yt(r){return new Date(A(r))}function wt(r,t=!1,e=!1){let n,i=r.getUTCFullYear();if(t){if(i<1950||2049<i)throw new Error(`not proper year for UTCTime: ${i}`);n=`${i}`.slice(-2)}else n=`000${i}`.slice(-4);if(n+=`0${r.getUTCMonth()+1}`.slice(-2),n+=`0${r.getUTCDate()}`.slice(-2),n+=`0${r.getUTCHours()}`.slice(-2),n+=`0${r.getUTCMinutes()}`.slice(-2),n+=`0${r.getUTCSeconds()}`.slice(-2),e){let o=r.getUTCMilliseconds();if(o!==0){let s=`00${o}`.slice(-3);s=s.replace(/0+$/g,""),n+=`.${s}`}}return n+="Z",n}function R(r){return r.match(/^[0-9]{12}Z$/)||r.match(/^[0-9]{12}[.][0-9]*Z$/)?r.match(/^[0-4]/)?`20${r}`:`19${r}`:r}function It(r){let t=r.match(/^(\d{2,4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})Z$/);if(!t)throw new Error("Invalid format: expected YYYYMMDDHHmmssZ or YYYYMMDDHHmmssZ");let[,e,n,i,o,s,l]=t,u=e;return u.length===2&&(parseInt(u.slice(0,1))<5?u=`20${u}`:u=`19${u}`),`${u}-${n}-${i}T${o}:${s}:${l}Z`}function At(r,t=!1){let e=r.match(/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z$/);if(!e)throw new Error("Invalid format: expected YYYY-MM-DDTHH:mm:ssZ");let[,n,i,o,s,l,u]=e,g=parseInt(n);if(t)if(1950<=g&&g<=2049)n=n.slice(2,4);else throw new Error(`Invalid year for UTCTime: ${n}`);return`${n}${i}${o}${s}${l}${u}Z`}var Bt=c(()=>{"use strict"});function B(r){let t=r,e="malformed IPv6 address";if(!t.match(/^[0-9A-Fa-f:]+$/))throw e;t=t.toLowerCase();let n=t.split(":").length-1;if(n<2)throw e;let i=":".repeat(7-n+2);t=t.replace("::",i);let o=t.split(":");if(o.length!==8)throw e;for(let s=0;s<8;s++)o[s]=`0000${o[s]}`.slice(-4);return o.join("")}function C(r){let t=r;if(!t.match(/^[0-9A-Fa-f]{32}$/))throw new Error(`malformed IPv6 address: ${t}`);t=t.toLowerCase();let e=t.match(/.{1,4}/g)||[];e=e.map(o=>o.replace(/^0+/,"")),e=e.map(o=>o===""?"0":o),t=`:${e.join(":")}:`;let n=t.match(/:(0:){2,}/g);if(n==null)return t.slice(1,-1);let i=n.sort().slice(-1)[0];return t=t.replace(i.substr(0,i.length-1),":"),t.substr(0,2)!=="::"&&(t=t.substr(1)),t.substr(-2,2)!=="::"&&(t=t.substr(0,t.length-1)),t}function k(r){let t=new Error("malformed hex value");if(!r.match(/^([0-9A-Fa-f][0-9A-Fa-f]){1,}$/))throw t;if(r.length===8){let e;try{return e=`${parseInt(r.substr(0,2),16)}.${parseInt(r.substr(2,2),16)}.${parseInt(r.substr(4,2),16)}.${parseInt(r.substr(6,2),16)}`,e}catch{throw t}}else if(r.length===16)try{return`${k(r.substr(0,8))}/${S(r.substr(8))}`}catch{throw t}else{if(r.length===32)return C(r);if(r.length===64)try{return`${C(r.substr(0,32))}/${S(r.substr(32))}`}catch{throw t}else return r}}function N(r){let t=r,e=new Error("malformed IP address");if(t=t.toLowerCase(),!t.match(/^[0-9a-f.:/]+$/))throw e;if(t.match(/^[0-9.]+$/)){let n=t.split(".");if(n.length!==4)throw e;let i="";try{for(let o=0;o<4;o++){let s=parseInt(n[o]);i+=`0${s.toString(16)}`.slice(-2)}return i}catch{throw e}}else if(t.match(/^[0-9.]+\/[0-9]+$/)){let n=t.split("/");return N(n[0])+E(parseInt(n[1]),32)}else{if(t.match(/^[0-9a-f:]+$/)&&t.indexOf(":")!==-1)return B(t);if(t.match(/^[0-9a-f:]+\/[0-9]+$/)&&t.indexOf(":")!==-1){let n=t.split("/");return B(n[0])+E(parseInt(n[1]),128)}else throw e}}function S(r){let t=new Error("malformed mask"),e;try{e=parseInt(r,16).toString(2)}catch{throw t}if(!e.match(/^1*0*$/))throw t;return e.replace(/0+$/,"").length}function E(r,t){if(t===32&&r===0)return"00000000";if(t===128&&r===0)return"00000000000000000000000000000000";let e=Array(r+1).join("1")+Array(t-r+1).join("0");return parseInt(e,2).toString(16)}var Ct=c(()=>{"use strict"});function St(r){function t(i){let o=parseInt(i.substr(0,2),16),s=parseInt(i.substr(2),16);if(o===0&&s<128)return String.fromCharCode(s);if(o<8){let b=192|(o&7)<<3|(s&192)>>6,f=128|s&63;return a(b.toString(16)+f.toString(16))}let l=224|(o&240)>>4,u=128|(o&15)<<2|(s&192)>>6,g=128|s&63;return a(l.toString(16)+u.toString(16)+g.toString(16))}let e=r.match(/.{4}/g);return e==null?"":e.map(t).join("")}var Et=c(()=>{"use strict";m()});function Ut(r){return r.length%2===1?`0${r}`:r}var Tt=c(()=>{"use strict"});var Dt={};Yt(Dt,{ArrayBuffertohex:()=>xt,BAtohex:()=>T,BAtos:()=>O,VERSION:()=>Ft,aryval:()=>nt,b64nltohex:()=>pt,b64tob64u:()=>w,b64tohex:()=>p,b64topem:()=>at,b64toutf8:()=>P,b64utob64:()=>h,b64utohex:()=>K,b64utoutf8:()=>G,binstrtobitstr:()=>X,binstrtonamearray:()=>et,bitstrtobinstr:()=>tt,datetozulu:()=>wt,encodeURIComponentAll:()=>V,foldnl:()=>I,hexpad:()=>Ut,hextoArrayBuffer:()=>mt,hextoBA:()=>F,hextob64:()=>x,hextob64u:()=>q,hextoip:()=>k,hextoipv6:()=>C,hextopem:()=>ft,hextorstr:()=>J,hextouricmp:()=>d,hextoutf8:()=>a,inttobitstr:()=>z,ipnetmask:()=>E,ipprefixlen:()=>S,iptohex:()=>N,ipv6tohex:()=>B,isBase64:()=>Y,isBase64URL:()=>it,ishex:()=>ot,iso8601tozulu:()=>At,iso88591hextoutf8:()=>ut,msectozulu:()=>$t,namearraytobinstr:()=>rt,pemtob64:()=>j,pemtohex:()=>gt,rstrtob64:()=>W,rstrtohex:()=>Q,stoBA:()=>D,stohex:()=>$,strpad:()=>Z,timetogen:()=>R,ucs2hextoutf8:()=>St,uricmptohex:()=>L,utf8tob64:()=>_,utf8tob64u:()=>H,utf8tohex:()=>y,utf8toiso88591hex:()=>lt,zulutodate:()=>yt,zulutoiso8601:()=>It,zulutomsec:()=>A,zulutosec:()=>dt});var Ft,vt=c(()=>{"use strict";st();v();m();ct();ht();bt();Bt();M();Ct();Et();Tt();Ft="typepki-strconv 0.11.0 kjur.github.io/typepki-strconv"});module.exports=(vt(),Rt(Dt));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typepki-strconv",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"author": "Kenji Urushima <kenji.urushima@gmail.com>",
|
|
5
5
|
"description": "TypeScript string converters and utilities sub module for TypePKI library (alpha version)",
|
|
6
6
|
"homepage": "https://kjur.github.io/typepki-strconv",
|
package/src/conv_pem.mts
CHANGED
|
@@ -26,6 +26,7 @@ export function hextopem(
|
|
|
26
26
|
return `-----BEGIN ${pemHeader}-----${separator}${foldnl(
|
|
27
27
|
hextob64(dataHex),
|
|
28
28
|
64,
|
|
29
|
+
separator
|
|
29
30
|
)}${separator}-----END ${pemHeader}-----${separator}`;
|
|
30
31
|
}
|
|
31
32
|
|
|
@@ -56,28 +57,55 @@ export function pemtohex(s: string, sHead?: string): string {
|
|
|
56
57
|
* get Base64 string from PEM format data
|
|
57
58
|
* @param pem - PEM formatted string
|
|
58
59
|
* @param sHead - PEM header string without BEGIN/END
|
|
59
|
-
* @return
|
|
60
|
+
* @return base64 string in the first PEM
|
|
60
61
|
*
|
|
61
62
|
* @description
|
|
62
63
|
* This static method gets a Base64 string of contents
|
|
63
|
-
* from PEM format data.
|
|
64
|
+
* from PEM format data. As for the "pem", some garbled text
|
|
65
|
+
* before and after the PEM header also be acceptable.
|
|
66
|
+
* When two or more PEMs are included, the first one will be
|
|
67
|
+
* returned.
|
|
64
68
|
*
|
|
65
69
|
* @example
|
|
66
70
|
* pemtob64("-----BEGIN CERTIFICATE...", "CERTIFICATE") -> "MIIBvTCC..."
|
|
71
|
+
* pemtob64("-----BEGIN CERTIFICATE...", "CERTIFICATE") -> "MIIBvTCC..."
|
|
67
72
|
* pemtob64("-----BEGIN CERTIFICATE...") -> "MIIBvTCC..."
|
|
73
|
+
* pem2 = `
|
|
74
|
+
* junk-text
|
|
75
|
+
* -----BEGIN FOO----
|
|
76
|
+
* YWFh
|
|
77
|
+
* -----BEGIN FOO----
|
|
78
|
+
* junk-text
|
|
79
|
+
* -----BEGIN BOO BOO----
|
|
80
|
+
* enp6
|
|
81
|
+
* -----BEGIN BOO BOO----
|
|
82
|
+
* junk-text
|
|
83
|
+
* `
|
|
84
|
+
* pemtob64(pem2) -> "YWFh"
|
|
68
85
|
*/
|
|
69
86
|
export function pemtob64(pem: string, sHead?: string): string {
|
|
70
87
|
let s: string = pem;
|
|
71
|
-
if (s.indexOf("-----BEGIN ") === -1) throw new Error("can't find PEM header");
|
|
72
88
|
|
|
73
89
|
if (sHead !== undefined) {
|
|
74
|
-
|
|
75
|
-
|
|
90
|
+
const sBegin = `-----BEGIN ${sHead}-----`;
|
|
91
|
+
const sEnd = `-----END ${sHead}-----`;
|
|
92
|
+
let idx = s.indexOf(sBegin);
|
|
93
|
+
if (idx == -1) throw new Error("can't find PEM header");
|
|
94
|
+
s = s.slice(idx + sBegin.length);
|
|
95
|
+
idx = s.indexOf(sEnd);
|
|
96
|
+
s = s.slice(0, idx);
|
|
97
|
+
return s.replace(/\s+/g, "");
|
|
76
98
|
} else {
|
|
77
|
-
|
|
78
|
-
|
|
99
|
+
const sBegin = `-----BEGIN `;
|
|
100
|
+
const sEnd = `-----END `;
|
|
101
|
+
let idx = s.indexOf(sBegin);
|
|
102
|
+
if (idx == -1) throw new Error("can't find PEM header");
|
|
103
|
+
s = s.slice(idx + sBegin.length);
|
|
104
|
+
s = s.replace(/^[0-9A-Za-z ]+-----/s, "");
|
|
105
|
+
idx = s.indexOf(sEnd);
|
|
106
|
+
s = s.slice(0, idx);
|
|
107
|
+
return s.replace(/\s+/g, "");
|
|
79
108
|
}
|
|
80
|
-
return s.replace(/\s+/g, "");
|
|
81
109
|
}
|
|
82
110
|
|
|
83
111
|
// === others =========================
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
hextopem,
|
|
5
|
+
pemtob64,
|
|
6
|
+
} from "./index.mts";
|
|
7
|
+
|
|
8
|
+
const PEM_FOO = `-----BEGIN FOO-----
|
|
9
|
+
YWFh
|
|
10
|
+
-----END FOO-----
|
|
11
|
+
`;
|
|
12
|
+
const PEM_FOO2_JUNK = `
|
|
13
|
+
junk text
|
|
14
|
+
junk text
|
|
15
|
+
junk text
|
|
16
|
+
-----BEGIN FOO FOO FOO 123-----
|
|
17
|
+
YWFh
|
|
18
|
+
-----END FOO FOO FOO 123-----
|
|
19
|
+
junk-text
|
|
20
|
+
-----BEGIN FOO FOO FOO 234-----
|
|
21
|
+
enp6
|
|
22
|
+
-----END FOO FOO FOO 234-----
|
|
23
|
+
junk-text
|
|
24
|
+
junk-text
|
|
25
|
+
`;
|
|
26
|
+
|
|
27
|
+
test("hextopem", () => {
|
|
28
|
+
expect(hextopem("616161", "FOO")).toBe(PEM_FOO.replace(/\n/g, "\r\n"));
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("pemtob64", () => {
|
|
32
|
+
expect(pemtob64(PEM_FOO)).toBe("YWFh");
|
|
33
|
+
expect(pemtob64(PEM_FOO2_JUNK)).toBe("YWFh");
|
|
34
|
+
expect(pemtob64(PEM_FOO2_JUNK, "FOO FOO FOO 123")).toBe("YWFh");
|
|
35
|
+
});
|
|
36
|
+
|
package/src/index.mts
CHANGED