@sphereon/ssi-sdk-ext.key-utils 0.10.2-next.20
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 +201 -0
- package/README.md +21 -0
- package/dist/digest-methods.d.ts +7 -0
- package/dist/digest-methods.d.ts.map +1 -0
- package/dist/digest-methods.js +95 -0
- package/dist/digest-methods.js.map +1 -0
- package/dist/functions.d.ts +30 -0
- package/dist/functions.d.ts.map +1 -0
- package/dist/functions.js +150 -0
- package/dist/functions.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/dist/jwk-jcs.d.ts +22 -0
- package/dist/jwk-jcs.d.ts.map +1 -0
- package/dist/jwk-jcs.js +178 -0
- package/dist/jwk-jcs.js.map +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +18 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/key-util-types.d.ts +36 -0
- package/dist/types/key-util-types.d.ts.map +1 -0
- package/dist/types/key-util-types.js +30 -0
- package/dist/types/key-util-types.js.map +1 -0
- package/dist/x509-utils.d.ts +24 -0
- package/dist/x509-utils.d.ts.map +1 -0
- package/dist/x509-utils.js +175 -0
- package/dist/x509-utils.js.map +1 -0
- package/package.json +49 -0
- package/src/digest-methods.ts +74 -0
- package/src/functions.ts +147 -0
- package/src/index.ts +11 -0
- package/src/jwk-jcs.ts +177 -0
- package/src/types/elliptic.d.ts +1 -0
- package/src/types/index.ts +1 -0
- package/src/types/key-util-types.ts +44 -0
- package/src/x509-utils.ts +145 -0
package/dist/jwk-jcs.js
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.jcsCanonicalize = exports.jwkJcsDecode = exports.jwkJcsEncode = void 0;
|
|
7
|
+
const web_encoding_1 = require("web-encoding");
|
|
8
|
+
const lodash_isplainobject_1 = __importDefault(require("lodash.isplainobject"));
|
|
9
|
+
const textEncoder = new web_encoding_1.TextEncoder();
|
|
10
|
+
const textDecoder = new web_encoding_1.TextDecoder();
|
|
11
|
+
/**
|
|
12
|
+
* Checks if the value is a non-empty string.
|
|
13
|
+
*
|
|
14
|
+
* @param value - The value to check.
|
|
15
|
+
* @param description - Description of the value to check.
|
|
16
|
+
*/
|
|
17
|
+
function check(value, description) {
|
|
18
|
+
if (typeof value !== 'string' || !value) {
|
|
19
|
+
throw new Error(`${description} missing or invalid`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Checks if the value is a valid JSON object.
|
|
24
|
+
*
|
|
25
|
+
* @param value - The value to check.
|
|
26
|
+
*/
|
|
27
|
+
function validatePlainObject(value) {
|
|
28
|
+
if (!(0, lodash_isplainobject_1.default)(value)) {
|
|
29
|
+
throw new Error('JWK must be an object');
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Checks if the JWK is valid. It must contain all the required members.
|
|
34
|
+
*
|
|
35
|
+
* @see https://www.rfc-editor.org/rfc/rfc7518#section-6
|
|
36
|
+
* @see https://www.rfc-editor.org/rfc/rfc8037#section-2
|
|
37
|
+
*
|
|
38
|
+
* @param jwk - The JWK to check.
|
|
39
|
+
*/
|
|
40
|
+
function validateJwk(jwk) {
|
|
41
|
+
validatePlainObject(jwk);
|
|
42
|
+
// Check JWK required members based on the key type
|
|
43
|
+
switch (jwk.kty) {
|
|
44
|
+
/**
|
|
45
|
+
* @see https://www.rfc-editor.org/rfc/rfc7518#section-6.2.1
|
|
46
|
+
*/
|
|
47
|
+
case 'EC':
|
|
48
|
+
check(jwk.crv, '"crv" (Curve) Parameter');
|
|
49
|
+
check(jwk.x, '"x" (X Coordinate) Parameter');
|
|
50
|
+
check(jwk.y, '"y" (Y Coordinate) Parameter');
|
|
51
|
+
break;
|
|
52
|
+
/**
|
|
53
|
+
* @see https://www.rfc-editor.org/rfc/rfc8037#section-2
|
|
54
|
+
*/
|
|
55
|
+
case 'OKP':
|
|
56
|
+
check(jwk.crv, '"crv" (Subtype of Key Pair) Parameter');
|
|
57
|
+
check(jwk.x, '"x" (Public Key) Parameter');
|
|
58
|
+
break;
|
|
59
|
+
/**
|
|
60
|
+
* @see https://www.rfc-editor.org/rfc/rfc7518#section-6.3.1
|
|
61
|
+
*/
|
|
62
|
+
case 'RSA':
|
|
63
|
+
check(jwk.e, '"e" (Exponent) Parameter');
|
|
64
|
+
check(jwk.n, '"n" (Modulus) Parameter');
|
|
65
|
+
break;
|
|
66
|
+
default:
|
|
67
|
+
throw new Error('"kty" (Key Type) Parameter missing or unsupported');
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Extracts the required members of the JWK and canonicalizes it.
|
|
72
|
+
*
|
|
73
|
+
* @param jwk - The JWK to canonicalize.
|
|
74
|
+
* @returns The JWK with only the required members, ordered lexicographically.
|
|
75
|
+
*/
|
|
76
|
+
function minimalJwk(jwk) {
|
|
77
|
+
// "default" case is not needed
|
|
78
|
+
// eslint-disable-next-line default-case
|
|
79
|
+
switch (jwk.kty) {
|
|
80
|
+
case 'EC':
|
|
81
|
+
return { crv: jwk.crv, kty: jwk.kty, x: jwk.x, y: jwk.y };
|
|
82
|
+
case 'OKP':
|
|
83
|
+
return { crv: jwk.crv, kty: jwk.kty, x: jwk.x };
|
|
84
|
+
case 'RSA':
|
|
85
|
+
return { e: jwk.e, kty: jwk.kty, n: jwk.n };
|
|
86
|
+
}
|
|
87
|
+
throw Error(`Unsupported key type (kty) provided: ${jwk.kty}`);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Encodes a JWK into a Uint8Array. Only the required JWK members are encoded.
|
|
91
|
+
*
|
|
92
|
+
* @see https://www.rfc-editor.org/rfc/rfc7518#section-6
|
|
93
|
+
* @see https://www.rfc-editor.org/rfc/rfc8037#section-2
|
|
94
|
+
* @see https://github.com/panva/jose/blob/3b8aa47b92d07a711bf5c3125276cc9a011794a4/src/jwk/thumbprint.ts#L37
|
|
95
|
+
*
|
|
96
|
+
* @param jwk - JSON Web Key.
|
|
97
|
+
* @returns Uint8Array-encoded JWK.
|
|
98
|
+
*/
|
|
99
|
+
function jwkJcsEncode(jwk) {
|
|
100
|
+
validateJwk(jwk);
|
|
101
|
+
const strippedJwk = minimalJwk(jwk);
|
|
102
|
+
return textEncoder.encode(jcsCanonicalize(strippedJwk));
|
|
103
|
+
}
|
|
104
|
+
exports.jwkJcsEncode = jwkJcsEncode;
|
|
105
|
+
/**
|
|
106
|
+
* Decodes an array of bytes into a JWK. Throws an error if the JWK is not valid.
|
|
107
|
+
*
|
|
108
|
+
* @param bytes - The array of bytes to decode.
|
|
109
|
+
* @returns The corresponding JSON Web Key.
|
|
110
|
+
*/
|
|
111
|
+
function jwkJcsDecode(bytes) {
|
|
112
|
+
const jwk = JSON.parse(textDecoder.decode(bytes));
|
|
113
|
+
validateJwk(jwk);
|
|
114
|
+
if (JSON.stringify(jwk) !== jcsCanonicalize(minimalJwk(jwk))) {
|
|
115
|
+
throw new Error('The JWK embedded in the DID is not correctly formatted');
|
|
116
|
+
}
|
|
117
|
+
return jwk;
|
|
118
|
+
}
|
|
119
|
+
exports.jwkJcsDecode = jwkJcsDecode;
|
|
120
|
+
// From: https://github.com/cyberphone/json-canonicalization
|
|
121
|
+
function jcsCanonicalize(object) {
|
|
122
|
+
let buffer = '';
|
|
123
|
+
serialize(object);
|
|
124
|
+
return buffer;
|
|
125
|
+
function serialize(object) {
|
|
126
|
+
if (object === null || typeof object !== 'object' || object.toJSON != null) {
|
|
127
|
+
/////////////////////////////////////////////////
|
|
128
|
+
// Primitive type or toJSON - Use ES6/JSON //
|
|
129
|
+
/////////////////////////////////////////////////
|
|
130
|
+
buffer += JSON.stringify(object);
|
|
131
|
+
}
|
|
132
|
+
else if (Array.isArray(object)) {
|
|
133
|
+
/////////////////////////////////////////////////
|
|
134
|
+
// Array - Maintain element order //
|
|
135
|
+
/////////////////////////////////////////////////
|
|
136
|
+
buffer += '[';
|
|
137
|
+
let next = false;
|
|
138
|
+
object.forEach((element) => {
|
|
139
|
+
if (next) {
|
|
140
|
+
buffer += ',';
|
|
141
|
+
}
|
|
142
|
+
next = true;
|
|
143
|
+
/////////////////////////////////////////
|
|
144
|
+
// Array element - Recursive expansion //
|
|
145
|
+
/////////////////////////////////////////
|
|
146
|
+
serialize(element);
|
|
147
|
+
});
|
|
148
|
+
buffer += ']';
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
/////////////////////////////////////////////////
|
|
152
|
+
// Object - Sort properties before serializing //
|
|
153
|
+
/////////////////////////////////////////////////
|
|
154
|
+
buffer += '{';
|
|
155
|
+
let next = false;
|
|
156
|
+
Object.keys(object)
|
|
157
|
+
.sort()
|
|
158
|
+
.forEach((property) => {
|
|
159
|
+
if (next) {
|
|
160
|
+
buffer += ',';
|
|
161
|
+
}
|
|
162
|
+
next = true;
|
|
163
|
+
///////////////////////////////////////////////
|
|
164
|
+
// Property names are strings - Use ES6/JSON //
|
|
165
|
+
///////////////////////////////////////////////
|
|
166
|
+
buffer += JSON.stringify(property);
|
|
167
|
+
buffer += ':';
|
|
168
|
+
//////////////////////////////////////////
|
|
169
|
+
// Property value - Recursive expansion //
|
|
170
|
+
//////////////////////////////////////////
|
|
171
|
+
serialize(object[property]);
|
|
172
|
+
});
|
|
173
|
+
buffer += '}';
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
exports.jcsCanonicalize = jcsCanonicalize;
|
|
178
|
+
//# sourceMappingURL=jwk-jcs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jwk-jcs.js","sourceRoot":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AAE;oBACR,MAAM,IAAI,GAAG,CAAA;iBACd;gBACD,IAAI,GAAG,IAAI,CAAA;gBACX,+CAA+C;gBAC/C,+CAA+C;gBAC/C,+CAA+C;gBAC/C,MAAM,IAAI,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAA;gBAClC,MAAM,IAAI,GAAG,CAAA;gBACb,0CAA0C;gBAC1C,0CAA0C;gBAC1C,0CAA0C;gBAC1C,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAA;YAC7B,CAAC,CAAC,CAAA;YACJ,MAAM,IAAI,GAAG,CAAA;SACd;IACH,CAAC;AACH,CAAC;AAtDD,0CAsDC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./key-util-types"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mDAAgC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export declare const JWK_JCS_PUB_NAME = "jwk_jcs-pub";
|
|
2
|
+
export declare const JWK_JCS_PUB_PREFIX = 60241;
|
|
3
|
+
export type TKeyType = 'Ed25519' | 'Secp256k1' | 'Secp256r1' | 'X25519' | 'Bls12381G1' | 'Bls12381G2' | 'RSA';
|
|
4
|
+
export declare enum Key {
|
|
5
|
+
Ed25519 = "Ed25519",
|
|
6
|
+
Secp256k1 = "Secp256k1",
|
|
7
|
+
Secp256r1 = "Secp256r1"
|
|
8
|
+
}
|
|
9
|
+
export declare enum JwkKeyUse {
|
|
10
|
+
Encryption = "enc",
|
|
11
|
+
Signature = "sig"
|
|
12
|
+
}
|
|
13
|
+
export declare enum KeyCurve {
|
|
14
|
+
Secp256k1 = "secp256k1",
|
|
15
|
+
P_256 = "P-256",
|
|
16
|
+
Ed25519 = "Ed25519"
|
|
17
|
+
}
|
|
18
|
+
export declare enum KeyType {
|
|
19
|
+
EC = "EC",
|
|
20
|
+
OKP = "OKP"
|
|
21
|
+
}
|
|
22
|
+
export declare const SIG_KEY_ALGS: string[];
|
|
23
|
+
export declare const ENC_KEY_ALGS: string[];
|
|
24
|
+
export interface JWK extends JsonWebKey {
|
|
25
|
+
x5c?: string;
|
|
26
|
+
x5u?: string;
|
|
27
|
+
}
|
|
28
|
+
export type KeyVisibility = 'public' | 'private';
|
|
29
|
+
export interface X509Opts {
|
|
30
|
+
cn?: string;
|
|
31
|
+
privateKeyPEM?: string;
|
|
32
|
+
certificatePEM?: string;
|
|
33
|
+
certificateChainURL?: string;
|
|
34
|
+
certificateChainPEM?: string;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=key-util-types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key-util-types.d.ts","sourceRoot":"","sources":["../../src/types/key-util-types.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,gBAAgB,gBAAgB,CAAA;AAC7C,eAAO,MAAM,kBAAkB,QAAS,CAAA;AAExC,MAAM,MAAM,QAAQ,GAAG,SAAS,GAAG,WAAW,GAAG,WAAW,GAAG,QAAQ,GAAG,YAAY,GAAG,YAAY,GAAG,KAAK,CAAA;AAE7G,oBAAY,GAAG;IACb,OAAO,YAAY;IACnB,SAAS,cAAc;IACvB,SAAS,cAAc;CACxB;AAED,oBAAY,SAAS;IACnB,UAAU,QAAQ;IAClB,SAAS,QAAQ;CAClB;AAED,oBAAY,QAAQ;IAClB,SAAS,cAAc;IACvB,KAAK,UAAU;IACf,OAAO,YAAY;CACpB;AAED,oBAAY,OAAO;IACjB,EAAE,OAAO;IACT,GAAG,QAAQ;CACZ;AAED,eAAO,MAAM,YAAY,UAAkH,CAAA;AAC3I,eAAO,MAAM,YAAY,UAA+C,CAAA;AAExE,MAAM,WAAW,GAAI,SAAQ,UAAU;IACrC,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,GAAG,CAAC,EAAE,MAAM,CAAA;CACb;AAED,MAAM,MAAM,aAAa,GAAG,QAAQ,GAAG,SAAS,CAAA;AAEhD,MAAM,WAAW,QAAQ;IACvB,EAAE,CAAC,EAAE,MAAM,CAAA;IACX,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,mBAAmB,CAAC,EAAE,MAAM,CAAA;IAC5B,mBAAmB,CAAC,EAAE,MAAM,CAAA;CAC7B"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ENC_KEY_ALGS = exports.SIG_KEY_ALGS = exports.KeyType = exports.KeyCurve = exports.JwkKeyUse = exports.Key = exports.JWK_JCS_PUB_PREFIX = exports.JWK_JCS_PUB_NAME = void 0;
|
|
4
|
+
exports.JWK_JCS_PUB_NAME = 'jwk_jcs-pub';
|
|
5
|
+
exports.JWK_JCS_PUB_PREFIX = 0xeb51;
|
|
6
|
+
var Key;
|
|
7
|
+
(function (Key) {
|
|
8
|
+
Key["Ed25519"] = "Ed25519";
|
|
9
|
+
Key["Secp256k1"] = "Secp256k1";
|
|
10
|
+
Key["Secp256r1"] = "Secp256r1";
|
|
11
|
+
})(Key = exports.Key || (exports.Key = {}));
|
|
12
|
+
var JwkKeyUse;
|
|
13
|
+
(function (JwkKeyUse) {
|
|
14
|
+
JwkKeyUse["Encryption"] = "enc";
|
|
15
|
+
JwkKeyUse["Signature"] = "sig";
|
|
16
|
+
})(JwkKeyUse = exports.JwkKeyUse || (exports.JwkKeyUse = {}));
|
|
17
|
+
var KeyCurve;
|
|
18
|
+
(function (KeyCurve) {
|
|
19
|
+
KeyCurve["Secp256k1"] = "secp256k1";
|
|
20
|
+
KeyCurve["P_256"] = "P-256";
|
|
21
|
+
KeyCurve["Ed25519"] = "Ed25519";
|
|
22
|
+
})(KeyCurve = exports.KeyCurve || (exports.KeyCurve = {}));
|
|
23
|
+
var KeyType;
|
|
24
|
+
(function (KeyType) {
|
|
25
|
+
KeyType["EC"] = "EC";
|
|
26
|
+
KeyType["OKP"] = "OKP";
|
|
27
|
+
})(KeyType = exports.KeyType || (exports.KeyType = {}));
|
|
28
|
+
exports.SIG_KEY_ALGS = ['ES256', 'ES384', 'ES512', 'EdDSA', 'ES256K', 'Ed25519', 'Secp256k1', 'Secp256r1', 'Bls12381G1', 'Bls12381G2'];
|
|
29
|
+
exports.ENC_KEY_ALGS = ['X25519', 'ECDH_ES_A256KW', 'RSA_OAEP_256'];
|
|
30
|
+
//# sourceMappingURL=key-util-types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key-util-types.js","sourceRoot":"","sources":["../../src/types/key-util-types.ts"],"names":[],"mappings":";;;AAAa,QAAA,gBAAgB,GAAG,aAAa,CAAA;AAChC,QAAA,kBAAkB,GAAG,MAAM,CAAA;AAIxC,IAAY,GAIX;AAJD,WAAY,GAAG;IACb,0BAAmB,CAAA;IACnB,8BAAuB,CAAA;IACvB,8BAAuB,CAAA;AACzB,CAAC,EAJW,GAAG,GAAH,WAAG,KAAH,WAAG,QAId;AAED,IAAY,SAGX;AAHD,WAAY,SAAS;IACnB,+BAAkB,CAAA;IAClB,8BAAiB,CAAA;AACnB,CAAC,EAHW,SAAS,GAAT,iBAAS,KAAT,iBAAS,QAGpB;AAED,IAAY,QAIX;AAJD,WAAY,QAAQ;IAClB,mCAAuB,CAAA;IACvB,2BAAe,CAAA;IACf,+BAAmB,CAAA;AACrB,CAAC,EAJW,QAAQ,GAAR,gBAAQ,KAAR,gBAAQ,QAInB;AAED,IAAY,OAGX;AAHD,WAAY,OAAO;IACjB,oBAAS,CAAA;IACT,sBAAW,CAAA;AACb,CAAC,EAHW,OAAO,GAAP,eAAO,KAAP,eAAO,QAGlB;AAEY,QAAA,YAAY,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,WAAW,EAAE,WAAW,EAAE,YAAY,EAAE,YAAY,CAAC,CAAA;AAC9H,QAAA,YAAY,GAAG,CAAC,QAAQ,EAAE,gBAAgB,EAAE,cAAc,CAAC,CAAA"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { JWK, KeyVisibility } from './types';
|
|
2
|
+
export declare function pemCertChainTox5c(cert: string, maxDepth?: number): string[];
|
|
3
|
+
export declare function x5cToPemCertChain(x5c: string[], maxDepth?: number): string;
|
|
4
|
+
export declare const toKeyObject: (PEM: string, visibility?: KeyVisibility) => {
|
|
5
|
+
pem: string;
|
|
6
|
+
jwk: JWK;
|
|
7
|
+
keyHex: string;
|
|
8
|
+
keyType: KeyVisibility;
|
|
9
|
+
};
|
|
10
|
+
export declare const jwkToPEM: (jwk: JWK, visibility?: KeyVisibility) => string;
|
|
11
|
+
export declare const PEMToJwk: (pem: string, visibility?: KeyVisibility) => JWK;
|
|
12
|
+
export declare const privateKeyHexFromPEM: (PEM: string) => string;
|
|
13
|
+
export declare const hexKeyFromPEMBasedJwk: (jwk: JWK, visibility?: KeyVisibility) => string;
|
|
14
|
+
export declare const publicKeyHexFromPEM: (PEM: string) => string;
|
|
15
|
+
export declare const PEMToHex: (PEM: string, headerKey?: string) => string;
|
|
16
|
+
/**
|
|
17
|
+
* Converts a base64 encoded string to hex string, removing any non-base64 characters, including newlines
|
|
18
|
+
* @param input The input in base64, with optional newlines
|
|
19
|
+
* @param inputEncoding
|
|
20
|
+
*/
|
|
21
|
+
export declare const base64ToHex: (input: string, inputEncoding?: 'base64pad' | 'base64urlpad') => string;
|
|
22
|
+
export declare const hexToPEM: (hex: string, type: KeyVisibility) => string;
|
|
23
|
+
export declare function base64ToPEM(cert: string, headerKey?: 'PUBLIC KEY' | 'RSA PRIVATE KEY' | 'PRIVATE KEY' | 'CERTIFICATE'): string;
|
|
24
|
+
//# sourceMappingURL=x509-utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"x509-utils.d.ts","sourceRoot":"","sources":["../src/x509-utils.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,GAAG,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAI5C,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAuB3E;AAED,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,CAU1E;AAED,eAAO,MAAM,WAAW,QAAS,MAAM,eAAc,aAAa;;;;;CAWjE,CAAA;AAED,eAAO,MAAM,QAAQ,QAAS,GAAG,eAAc,aAAa,KAAc,MAEzE,CAAA;AAED,eAAO,MAAM,QAAQ,QAAS,MAAM,eAAc,aAAa,KAAc,GAE5E,CAAA;AACD,eAAO,MAAM,oBAAoB,QAAS,MAAM,WAE/C,CAAA;AAED,eAAO,MAAM,qBAAqB,QAAS,GAAG,eAAc,aAAa,KAAc,MAMtF,CAAA;AAED,eAAO,MAAM,mBAAmB,QAAS,MAAM,WAU9C,CAAA;AAED,eAAO,MAAM,QAAQ,QAAS,MAAM,cAAc,MAAM,KAAG,MAc1D,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,WAAW,UAAW,MAAM,kBAAkB,WAAW,GAAG,cAAc,WAGtF,CAAA;AAUD,eAAO,MAAM,QAAQ,QAAS,MAAM,QAAQ,aAAa,KAAG,MAa3D,CAAA;AAED,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,YAAY,GAAG,iBAAiB,GAAG,aAAa,GAAG,aAAa,GAAG,MAAM,CAO9H"}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.base64ToPEM = exports.hexToPEM = exports.base64ToHex = exports.PEMToHex = exports.publicKeyHexFromPEM = exports.hexKeyFromPEMBasedJwk = exports.privateKeyHexFromPEM = exports.PEMToJwk = exports.jwkToPEM = exports.toKeyObject = exports.x5cToPemCertChain = exports.pemCertChainTox5c = void 0;
|
|
30
|
+
const u8a = __importStar(require("uint8arrays"));
|
|
31
|
+
// @ts-ignore
|
|
32
|
+
const keyto_1 = __importDefault(require("@trust/keyto"));
|
|
33
|
+
// Based on (MIT licensed):
|
|
34
|
+
// https://github.com/hildjj/node-posh/blob/master/lib/index.js
|
|
35
|
+
function pemCertChainTox5c(cert, maxDepth) {
|
|
36
|
+
if (!maxDepth) {
|
|
37
|
+
maxDepth = 0;
|
|
38
|
+
}
|
|
39
|
+
/*
|
|
40
|
+
* Convert a PEM-encoded certificate to the version used in the x5c element
|
|
41
|
+
* of a [JSON Web Key](http://tools.ietf.org/html/draft-ietf-jose-json-web-key).
|
|
42
|
+
*
|
|
43
|
+
* `cert` PEM-encoded certificate chain
|
|
44
|
+
* `maxdepth` The maximum number of certificates to use from the chain.
|
|
45
|
+
*/
|
|
46
|
+
const intermediate = cert
|
|
47
|
+
.replace(/-----[^\n]+\n?/gm, ',')
|
|
48
|
+
.replace(/\n/g, '')
|
|
49
|
+
.replace(/\r/g, '');
|
|
50
|
+
let x5c = intermediate.split(',').filter(function (c) {
|
|
51
|
+
return c.length > 0;
|
|
52
|
+
});
|
|
53
|
+
if (maxDepth > 0) {
|
|
54
|
+
x5c = x5c.splice(0, maxDepth);
|
|
55
|
+
}
|
|
56
|
+
return x5c;
|
|
57
|
+
}
|
|
58
|
+
exports.pemCertChainTox5c = pemCertChainTox5c;
|
|
59
|
+
function x5cToPemCertChain(x5c, maxDepth) {
|
|
60
|
+
if (!maxDepth) {
|
|
61
|
+
maxDepth = 0;
|
|
62
|
+
}
|
|
63
|
+
const length = maxDepth === 0 ? x5c.length : Math.min(maxDepth, x5c.length);
|
|
64
|
+
let pem = '';
|
|
65
|
+
for (let i = 0; i < length; i++) {
|
|
66
|
+
pem += base64ToPEM(x5c[i], 'CERTIFICATE');
|
|
67
|
+
}
|
|
68
|
+
return pem;
|
|
69
|
+
}
|
|
70
|
+
exports.x5cToPemCertChain = x5cToPemCertChain;
|
|
71
|
+
const toKeyObject = (PEM, visibility = 'public') => {
|
|
72
|
+
const jwk = (0, exports.PEMToJwk)(PEM, visibility);
|
|
73
|
+
const keyVisibility = jwk.d ? 'private' : 'public';
|
|
74
|
+
const keyHex = keyVisibility === 'private' ? (0, exports.privateKeyHexFromPEM)(PEM) : (0, exports.publicKeyHexFromPEM)(PEM);
|
|
75
|
+
return {
|
|
76
|
+
pem: (0, exports.hexToPEM)(keyHex, visibility),
|
|
77
|
+
jwk,
|
|
78
|
+
keyHex,
|
|
79
|
+
keyType: keyVisibility,
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
exports.toKeyObject = toKeyObject;
|
|
83
|
+
const jwkToPEM = (jwk, visibility = 'public') => {
|
|
84
|
+
return keyto_1.default.from(jwk, 'jwk').toString('pem', visibility === 'public' ? 'public_pkcs8' : 'private_pkcs8');
|
|
85
|
+
};
|
|
86
|
+
exports.jwkToPEM = jwkToPEM;
|
|
87
|
+
const PEMToJwk = (pem, visibility = 'public') => {
|
|
88
|
+
return keyto_1.default.from(pem, 'pem').toJwk(visibility);
|
|
89
|
+
};
|
|
90
|
+
exports.PEMToJwk = PEMToJwk;
|
|
91
|
+
const privateKeyHexFromPEM = (PEM) => {
|
|
92
|
+
return (0, exports.PEMToHex)(PEM);
|
|
93
|
+
};
|
|
94
|
+
exports.privateKeyHexFromPEM = privateKeyHexFromPEM;
|
|
95
|
+
const hexKeyFromPEMBasedJwk = (jwk, visibility = 'public') => {
|
|
96
|
+
if (visibility === 'private') {
|
|
97
|
+
return (0, exports.privateKeyHexFromPEM)((0, exports.jwkToPEM)(jwk, 'private'));
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
return (0, exports.publicKeyHexFromPEM)((0, exports.jwkToPEM)(jwk, 'public'));
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
exports.hexKeyFromPEMBasedJwk = hexKeyFromPEMBasedJwk;
|
|
104
|
+
const publicKeyHexFromPEM = (PEM) => {
|
|
105
|
+
const hex = (0, exports.PEMToHex)(PEM);
|
|
106
|
+
if (PEM.includes('CERTIFICATE')) {
|
|
107
|
+
throw Error('Cannot directly deduce public Key from PEM Certificate yet');
|
|
108
|
+
}
|
|
109
|
+
else if (!PEM.includes('PRIVATE')) {
|
|
110
|
+
return hex;
|
|
111
|
+
}
|
|
112
|
+
const publicJwk = (0, exports.PEMToJwk)(PEM, 'public');
|
|
113
|
+
const publicPEM = (0, exports.jwkToPEM)(publicJwk, 'public');
|
|
114
|
+
return (0, exports.PEMToHex)(publicPEM);
|
|
115
|
+
};
|
|
116
|
+
exports.publicKeyHexFromPEM = publicKeyHexFromPEM;
|
|
117
|
+
const PEMToHex = (PEM, headerKey) => {
|
|
118
|
+
if (PEM.indexOf('-----BEGIN ') == -1) {
|
|
119
|
+
throw Error(`PEM header not found: ${headerKey}`);
|
|
120
|
+
}
|
|
121
|
+
let strippedPem;
|
|
122
|
+
if (headerKey) {
|
|
123
|
+
strippedPem = PEM.replace(new RegExp('^[^]*-----BEGIN ' + headerKey + '-----'), '');
|
|
124
|
+
strippedPem = strippedPem.replace(new RegExp('-----END ' + headerKey + '-----[^]*$'), '');
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
strippedPem = PEM.replace(/^[^]*-----BEGIN [^-]+-----/, '');
|
|
128
|
+
strippedPem = strippedPem.replace(/-----END [^-]+-----[^]*$/, '');
|
|
129
|
+
}
|
|
130
|
+
return (0, exports.base64ToHex)(strippedPem, 'base64pad');
|
|
131
|
+
};
|
|
132
|
+
exports.PEMToHex = PEMToHex;
|
|
133
|
+
/**
|
|
134
|
+
* Converts a base64 encoded string to hex string, removing any non-base64 characters, including newlines
|
|
135
|
+
* @param input The input in base64, with optional newlines
|
|
136
|
+
* @param inputEncoding
|
|
137
|
+
*/
|
|
138
|
+
const base64ToHex = (input, inputEncoding) => {
|
|
139
|
+
const base64NoNewlines = input.replace(/[^0-9A-Za-z\/+=]*/g, '');
|
|
140
|
+
return u8a.toString(u8a.fromString(base64NoNewlines, inputEncoding ? inputEncoding : 'base64pad'), 'base16');
|
|
141
|
+
};
|
|
142
|
+
exports.base64ToHex = base64ToHex;
|
|
143
|
+
const hexToBase64 = (input, targetEncoding) => {
|
|
144
|
+
let hex = typeof input === 'string' ? input : input.toString(16);
|
|
145
|
+
if (hex.length % 2 === 1) {
|
|
146
|
+
hex = `0${hex}`;
|
|
147
|
+
}
|
|
148
|
+
return u8a.toString(u8a.fromString(hex, 'base16'), targetEncoding ? targetEncoding : 'base64pad');
|
|
149
|
+
};
|
|
150
|
+
const hexToPEM = (hex, type) => {
|
|
151
|
+
const base64 = hexToBase64(hex, 'base64pad');
|
|
152
|
+
const headerKey = type === 'private' ? 'RSA PRIVATE KEY' : 'PUBLIC KEY';
|
|
153
|
+
if (type === 'private') {
|
|
154
|
+
const pem = base64ToPEM(base64, headerKey);
|
|
155
|
+
try {
|
|
156
|
+
(0, exports.PEMToJwk)(pem); // We only use it to test the private key
|
|
157
|
+
return pem;
|
|
158
|
+
}
|
|
159
|
+
catch (error) {
|
|
160
|
+
return base64ToPEM(base64, 'PRIVATE KEY');
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return base64ToPEM(base64, headerKey);
|
|
164
|
+
};
|
|
165
|
+
exports.hexToPEM = hexToPEM;
|
|
166
|
+
function base64ToPEM(cert, headerKey) {
|
|
167
|
+
const key = headerKey !== null && headerKey !== void 0 ? headerKey : 'CERTIFICATE';
|
|
168
|
+
const matches = cert.match(/.{1,64}/g);
|
|
169
|
+
if (!matches) {
|
|
170
|
+
throw Error('Invalid cert input value supplied');
|
|
171
|
+
}
|
|
172
|
+
return `-----BEGIN ${key}-----\n${matches.join('\n')}\n-----END ${key}-----\n`;
|
|
173
|
+
}
|
|
174
|
+
exports.base64ToPEM = base64ToPEM;
|
|
175
|
+
//# sourceMappingURL=x509-utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"x509-utils.js","sourceRoot":"","sources":["../src/x509-utils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAAkC;AAClC,aAAa;AACb,yDAAgC;AAGhC,2BAA2B;AAC3B,+DAA+D;AAC/D,SAAgB,iBAAiB,CAAC,IAAY,EAAE,QAAiB;IAC/D,IAAI,CAAC,QAAQ,EAAE;QACb,QAAQ,GAAG,CAAC,CAAA;KACb;IACD;;;;;;OAMG;IAEH,MAAM,YAAY,GAAG,IAAI;SACtB,OAAO,CAAC,kBAAkB,EAAE,GAAG,CAAC;SAChC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC;SAClB,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAA;IACrB,IAAI,GAAG,GAAG,YAAY,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC;QAClD,OAAO,CAAC,CAAC,MAAM,GAAG,CAAC,CAAA;IACrB,CAAC,CAAC,CAAA;IACF,IAAI,QAAQ,GAAG,CAAC,EAAE;QAChB,GAAG,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAA;KAC9B;IACD,OAAO,GAAG,CAAA;AACZ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AY,EAAE,SAA4E;IACpH,MAAM,GAAG,GAAG,SAAS,aAAT,SAAS,cAAT,SAAS,GAAI,aAAa,CAAA;IACtC,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAA;IACtC,IAAI,CAAC,OAAO,EAAE;QACZ,MAAM,KAAK,CAAC,mCAAmC,CAAC,CAAA;KACjD;IACD,OAAO,cAAc,GAAG,UAAU,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,cAAc,GAAG,SAAS,CAAA;AAChF,CAAC;AAPD,kCAOC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sphereon/ssi-sdk-ext.key-utils",
|
|
3
|
+
"description": "Sphereon SSI-SDK plugin for key creation.",
|
|
4
|
+
"version": "0.10.2-next.20+fe74ec5",
|
|
5
|
+
"source": "src/index.ts",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc --build",
|
|
10
|
+
"build:clean": "tsc --build --clean && tsc --build"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@ethersproject/random": "^5.6.1",
|
|
14
|
+
"@stablelib/ed25519": "^1.0.2",
|
|
15
|
+
"@stablelib/sha256": "^1.0.1",
|
|
16
|
+
"@stablelib/sha512": "^1.0.1",
|
|
17
|
+
"base64url": "^3.0.1",
|
|
18
|
+
"debug": "^4.3.4",
|
|
19
|
+
"did-resolver": "^4.1.0",
|
|
20
|
+
"elliptic": "^6.5.4",
|
|
21
|
+
"lodash.isplainobject": "^4.0.6",
|
|
22
|
+
"multiformats": "^9.9.0",
|
|
23
|
+
"uint8arrays": "^3.1.1",
|
|
24
|
+
"varint": "^6.0.0",
|
|
25
|
+
"web-encoding": "^1.1.5"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/lodash.isplainobject": "^4.0.7"
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist/**/*",
|
|
32
|
+
"src/**/*",
|
|
33
|
+
"README.md",
|
|
34
|
+
"LICENSE"
|
|
35
|
+
],
|
|
36
|
+
"private": false,
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"repository": "git@github.com:Sphereon-OpenSource/ssi-sdk.git",
|
|
41
|
+
"author": "Sphereon <dev@sphereon.com>",
|
|
42
|
+
"license": "Apache-2.0",
|
|
43
|
+
"keywords": [
|
|
44
|
+
"JWK",
|
|
45
|
+
"DID",
|
|
46
|
+
"Veramo"
|
|
47
|
+
],
|
|
48
|
+
"gitHead": "fe74ec598881102070db75141155719cc9df9812"
|
|
49
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { hash as sha256 } from '@stablelib/sha256'
|
|
2
|
+
import { hash as sha512 } from '@stablelib/sha512'
|
|
3
|
+
import * as u8a from 'uint8arrays'
|
|
4
|
+
|
|
5
|
+
export type HashAlgorithm = 'SHA-256' | 'SHA-512'
|
|
6
|
+
export type TDigestMethod = (input: string) => string
|
|
7
|
+
|
|
8
|
+
export const digestMethodParams = (hashAlgorithm: HashAlgorithm): { hashAlgorithm: HashAlgorithm; digestMethod: TDigestMethod } => {
|
|
9
|
+
if (hashAlgorithm === 'SHA-256') {
|
|
10
|
+
return { hashAlgorithm: 'SHA-256', digestMethod: sha256DigestMethod }
|
|
11
|
+
} else {
|
|
12
|
+
return { hashAlgorithm: 'SHA-512', digestMethod: sha512DigestMethod }
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const sha256DigestMethod = (input: string): string => {
|
|
17
|
+
return u8a.toString(sha256(u8a.fromString(input, 'utf-8')), 'base16')
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const sha512DigestMethod = (input: string): string => {
|
|
21
|
+
return u8a.toString(sha512(u8a.fromString(input, 'utf-8')), 'base16')
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/*
|
|
25
|
+
// PKCS#1 (PSS) mask generation function
|
|
26
|
+
function pss_mgf1_str(seed, len, hash) {
|
|
27
|
+
var mask = '', i = 0;
|
|
28
|
+
|
|
29
|
+
while (mask.length < len) {
|
|
30
|
+
mask += hextorstr(hash(rstrtohex(seed + String.fromCharCode.apply(String, [
|
|
31
|
+
(i & 0xff000000) >> 24,
|
|
32
|
+
(i & 0x00ff0000) >> 16,
|
|
33
|
+
(i & 0x0000ff00) >> 8,
|
|
34
|
+
i & 0x000000ff]))));
|
|
35
|
+
i += 1;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return mask;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
/*
|
|
44
|
+
|
|
45
|
+
/!**
|
|
46
|
+
* Generate mask of specified length.
|
|
47
|
+
*
|
|
48
|
+
* @param {String} seed The seed for mask generation.
|
|
49
|
+
* @param maskLen Number of bytes to generate.
|
|
50
|
+
* @return {String} The generated mask.
|
|
51
|
+
*!/
|
|
52
|
+
export const mgf1 = (dm: TDigestMethod, seed: string, maskLen: number) => {
|
|
53
|
+
/!* 2. Let T be the empty octet string. *!/
|
|
54
|
+
var t = new forge.util.ByteBuffer();
|
|
55
|
+
|
|
56
|
+
/!* 3. For counter from 0 to ceil(maskLen / hLen), do the following: *!/
|
|
57
|
+
var len = Math.ceil(maskLen / md.digestLength);
|
|
58
|
+
for(var i = 0; i < len; i++) {
|
|
59
|
+
/!* a. Convert counter to an octet string C of length 4 octets *!/
|
|
60
|
+
var c = new forge.util.ByteBuffer();
|
|
61
|
+
c.putInt32(i);
|
|
62
|
+
|
|
63
|
+
/!* b. Concatenate the hash of the seed mgfSeed and C to the octet
|
|
64
|
+
* string T: *!/
|
|
65
|
+
md.start();
|
|
66
|
+
md.update(seed + c.getBytes());
|
|
67
|
+
t.putBuffer(md.digest());
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/!* Output the leading maskLen octets of T as the octet string mask. *!/
|
|
71
|
+
t.truncate(t.length() - maskLen);
|
|
72
|
+
return t.getBytes();
|
|
73
|
+
}
|
|
74
|
+
*/
|