@api-client/core 0.3.2 → 0.3.5
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/build/browser.d.ts +1 -0
- package/build/browser.js +1 -0
- package/build/browser.js.map +1 -1
- package/build/index.d.ts +1 -1
- package/build/index.js +1 -1
- package/build/index.js.map +1 -1
- package/build/src/models/ErrorResponse.d.ts +5 -4
- package/build/src/models/ErrorResponse.js +18 -5
- package/build/src/models/ErrorResponse.js.map +1 -1
- package/build/src/models/SerializableError.d.ts +30 -0
- package/build/src/models/SerializableError.js +63 -0
- package/build/src/models/SerializableError.js.map +1 -0
- package/build/src/runtime/http-engine/ArcEngine.js +8 -4
- package/build/src/runtime/http-engine/ArcEngine.js.map +1 -1
- package/build/src/runtime/http-engine/HttpEngine.d.ts +3 -3
- package/build/src/runtime/http-engine/HttpEngine.js +3 -3
- package/build/src/runtime/http-engine/HttpEngine.js.map +1 -1
- package/build/src/runtime/http-engine/NodeEngine.js +9 -4
- package/build/src/runtime/http-engine/NodeEngine.js.map +1 -1
- package/build/src/runtime/http-engine/NodeEngineDirect.js +8 -2
- package/build/src/runtime/http-engine/NodeEngineDirect.js.map +1 -1
- package/build/src/runtime/node/ProjectRunner.d.ts +2 -2
- package/build/src/runtime/node/ProjectRunner.js +9 -2
- package/build/src/runtime/node/ProjectRunner.js.map +1 -1
- package/build/src/runtime/store/StoreSdk.js +10 -15
- package/build/src/runtime/store/StoreSdk.js.map +1 -1
- package/package.json +1 -1
- package/src/data/DataReader.ts +11 -0
- package/src/data/DataUtils.ts +108 -0
- package/src/data/JmesparthReader.ts +26 -0
- package/src/data/Json2Xml.ts +190 -0
- package/src/data/JsonReader.ts +41 -0
- package/src/data/PayloadPointer.ts +48 -0
- package/src/data/RequestDataExtractor.ts +133 -0
- package/src/data/UrlEncodedReader.ts +20 -0
- package/src/data/XmlReader.ts +103 -0
- package/src/events/BaseEvents.ts +259 -0
- package/src/events/CustomEvent.ts +27 -0
- package/src/events/EventTypes.ts +19 -0
- package/src/events/Events.ts +19 -0
- package/src/events/authorization/AuthorizationEventTypes.ts +22 -0
- package/src/events/authorization/AuthorizationEvents.ts +61 -0
- package/src/events/cookies/CookieEventTypes.ts +13 -0
- package/src/events/cookies/CookieEvents.ts +157 -0
- package/src/events/encryption/EncryptionEventTypes.ts +4 -0
- package/src/events/encryption/EncryptionEvents.ts +51 -0
- package/src/events/environment/EnvironmentEventTypes.ts +3 -0
- package/src/events/environment/EnvironmentEvents.ts +24 -0
- package/src/events/models/ClientCertificateEvents.ts +87 -0
- package/src/events/models/ModelEventTypes.ts +47 -0
- package/src/events/models/ModelEvents.ts +7 -0
- package/src/events/models/ProjectEvents.ts +331 -0
- package/src/events/process/ProcessEventTypes.ts +5 -0
- package/src/events/process/ProcessEvents.ts +76 -0
- package/src/events/readme.md +22 -0
- package/src/events/reporting/ReportingEventTypes.ts +3 -0
- package/src/events/reporting/ReportingEvents.ts +28 -0
- package/src/events/telemetry/TelemetryEventTypes.ts +10 -0
- package/src/events/telemetry/TelemetryEvents.ts +156 -0
- package/src/lib/cookies/Cookie.ts +312 -0
- package/src/lib/cookies/Cookies.ts +326 -0
- package/src/lib/cookies/Utils.ts +168 -0
- package/src/lib/headers/Headers.ts +219 -0
- package/src/lib/logging/DefaultLogger.ts +19 -0
- package/src/lib/logging/DummyLogger.ts +21 -0
- package/src/lib/logging/Logger.ts +16 -0
- package/src/lib/transformers/PayloadSerializer.ts +332 -0
- package/src/lib/transformers/Utils.ts +18 -0
- package/src/lib/uuid.ts +40 -0
- package/src/mocking/LegacyInterfaces.ts +52 -0
- package/src/mocking/LegacyMock.ts +37 -0
- package/src/mocking/legacy/Authorization.ts +39 -0
- package/src/mocking/legacy/Certificates.ts +145 -0
- package/src/mocking/legacy/Cookies.ts +51 -0
- package/src/mocking/legacy/HostRules.ts +43 -0
- package/src/mocking/legacy/Http.ts +236 -0
- package/src/mocking/legacy/HttpResponse.ts +106 -0
- package/src/mocking/legacy/RestApi.ts +68 -0
- package/src/mocking/legacy/Urls.ts +44 -0
- package/src/mocking/legacy/Variables.ts +53 -0
- package/src/models/ArcResponse.ts +166 -0
- package/src/models/Authorization.ts +481 -0
- package/src/models/AuthorizationData.ts +60 -0
- package/src/models/Backend.ts +107 -0
- package/src/models/ClientCertificate.ts +68 -0
- package/src/models/Environment.ts +279 -0
- package/src/models/ErrorResponse.ts +113 -0
- package/src/models/HistoryIndex.ts +76 -0
- package/src/models/HistoryRequest.ts +28 -0
- package/src/models/HostRule.ts +163 -0
- package/src/models/HttpCookie.ts +285 -0
- package/src/models/HttpProject.ts +1294 -0
- package/src/models/HttpProjectListItem.ts +23 -0
- package/src/models/HttpRequest.ts +124 -0
- package/src/models/HttpResponse.ts +143 -0
- package/src/models/License.ts +113 -0
- package/src/models/ProjectDefinitionProperty.ts +40 -0
- package/src/models/ProjectFolder.ts +439 -0
- package/src/models/ProjectItem.ts +135 -0
- package/src/models/ProjectParent.ts +113 -0
- package/src/models/ProjectRequest.ts +277 -0
- package/src/models/ProjectSchema.ts +202 -0
- package/src/models/Property.ts +423 -0
- package/src/models/Provider.ts +98 -0
- package/src/models/README.md +20 -0
- package/src/models/Request.ts +452 -0
- package/src/models/RequestActions.ts +163 -0
- package/src/models/RequestAuthorization.ts +115 -0
- package/src/models/RequestConfig.ts +317 -0
- package/src/models/RequestLog.ts +159 -0
- package/src/models/RequestTime.ts +108 -0
- package/src/models/RequestUiMeta.ts +258 -0
- package/src/models/RequestsSize.ts +65 -0
- package/src/models/ResponseAuthorization.ts +104 -0
- package/src/models/ResponseRedirect.ts +158 -0
- package/src/models/RevisionInfo.ts +37 -0
- package/src/models/SentRequest.ts +125 -0
- package/src/models/SerializableError.ts +80 -0
- package/src/models/SerializablePayload.ts +68 -0
- package/src/models/Server.ts +153 -0
- package/src/models/Thing.ts +110 -0
- package/src/models/Url.ts +90 -0
- package/src/models/User.ts +120 -0
- package/src/models/WebApi.ts +234 -0
- package/src/models/WebApiIndex.ts +122 -0
- package/src/models/Workspace.ts +182 -0
- package/src/models/actions/Action.ts +213 -0
- package/src/models/actions/ActionView.ts +40 -0
- package/src/models/actions/Condition.ts +207 -0
- package/src/models/actions/ConditionView.ts +42 -0
- package/src/models/actions/Enums.ts +29 -0
- package/src/models/actions/RunnableAction.ts +144 -0
- package/src/models/actions/runnable/DeleteCookieAction.ts +113 -0
- package/src/models/actions/runnable/Runnable.ts +9 -0
- package/src/models/actions/runnable/SetCookieAction.ts +216 -0
- package/src/models/actions/runnable/SetVariableAction.ts +81 -0
- package/src/models/legacy/DataExport.ts +172 -0
- package/src/models/legacy/Normalizer.ts +110 -0
- package/src/models/legacy/actions/Actions.ts +269 -0
- package/src/models/legacy/authorization/Authorization.ts +572 -0
- package/src/models/legacy/models/ApiTypes.ts +202 -0
- package/src/models/legacy/models/ArcLegacyProject.ts +39 -0
- package/src/models/legacy/models/AuthData.ts +17 -0
- package/src/models/legacy/models/ClientCertificate.ts +95 -0
- package/src/models/legacy/models/Cookies.ts +52 -0
- package/src/models/legacy/models/HostRule.ts +35 -0
- package/src/models/legacy/models/RestApi.ts +49 -0
- package/src/models/legacy/models/UrlHistory.ts +37 -0
- package/src/models/legacy/models/Variable.ts +43 -0
- package/src/models/legacy/models/base.d.ts +95 -0
- package/src/models/legacy/request/ArcRequest.ts +405 -0
- package/src/models/legacy/request/ArcResponse.ts +177 -0
- package/src/models/legacy/request/HistoryData.ts +47 -0
- package/src/models/legacy/request/Legacy.ts +45 -0
- package/src/models/legacy/request/RequestBody.ts +87 -0
- package/src/models/transformers/ArcDexieTransformer.ts +323 -0
- package/src/models/transformers/ArcLegacyNormalizer.ts +85 -0
- package/src/models/transformers/ArcLegacyTransformer.ts +200 -0
- package/src/models/transformers/ArcPouchTransformer.ts +184 -0
- package/src/models/transformers/BaseTransformer.ts +116 -0
- package/src/models/transformers/ImportUtils.ts +141 -0
- package/src/models/transformers/LegacyDataExportToApiProject.ts +76 -0
- package/src/models/transformers/LegacyExportProcessor.ts +252 -0
- package/src/models/transformers/PostmanBackupTransformer.ts +306 -0
- package/src/models/transformers/PostmanDataTransformer.ts +50 -0
- package/src/models/transformers/PostmanTransformer.ts +106 -0
- package/src/models/transformers/PostmanV21Transformer.ts +311 -0
- package/src/models/transformers/PostmanV2Transformer.ts +308 -0
- package/src/models/transformers/har.ts +865 -0
- package/src/runtime/actions/ActionRunner.ts +83 -0
- package/src/runtime/actions/ConditionRunner.ts +194 -0
- package/src/runtime/actions/RunnableCondition.ts +57 -0
- package/src/runtime/actions/runnable/ActionRunnable.ts +19 -0
- package/src/runtime/actions/runnable/DeleteCookieRunnable.ts +39 -0
- package/src/runtime/actions/runnable/SetCookieRunnable.ts +92 -0
- package/src/runtime/actions/runnable/SetVariableRunnable.ts +53 -0
- package/src/runtime/http-engine/ArcEngine.ts +1068 -0
- package/src/runtime/http-engine/FormData.ts +85 -0
- package/src/runtime/http-engine/HttpEngine.ts +874 -0
- package/src/runtime/http-engine/HttpErrorCodes.ts +270 -0
- package/src/runtime/http-engine/NodeEngine.ts +792 -0
- package/src/runtime/http-engine/NodeEngineDirect.ts +482 -0
- package/src/runtime/http-engine/PayloadSupport.ts +84 -0
- package/src/runtime/http-engine/RequestUtils.ts +164 -0
- package/src/runtime/http-engine/ntlm/Des.ts +345 -0
- package/src/runtime/http-engine/ntlm/MD4.ts +135 -0
- package/src/runtime/http-engine/ntlm/NtlmAuth.ts +186 -0
- package/src/runtime/http-engine/ntlm/NtlmMessage.ts +57 -0
- package/src/runtime/modules/BasicAuthCache.ts +133 -0
- package/src/runtime/modules/ExecutionResponse.ts +4 -0
- package/src/runtime/modules/ModulesRegistry.ts +136 -0
- package/src/runtime/modules/RequestAuthorization.ts +110 -0
- package/src/runtime/modules/RequestCookies.ts +145 -0
- package/src/runtime/node/ProjectRunner.ts +281 -0
- package/src/runtime/node/RequestFactory.ts +422 -0
- package/src/runtime/node/VariablesStore.ts +25 -0
- package/src/runtime/store/StoreSdk.ts +838 -0
- package/src/runtime/variables/Cache.ts +53 -0
- package/src/runtime/variables/EvalFunctions.ts +132 -0
- package/src/runtime/variables/ProjectVariables.ts +6 -0
- package/src/runtime/variables/VariablesProcessor.ts +543 -0
- package/src/runtime/variables/VariablesTokenizer.ts +55 -0
- package/build/src/runtime/http-engine/Errors.d.ts +0 -10
- package/build/src/runtime/http-engine/Errors.js +0 -14
- package/build/src/runtime/http-engine/Errors.js.map +0 -1
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { Headers } from '../../lib/headers/Headers.js';
|
|
2
|
+
import { ResponseRedirect } from '../../models/ResponseRedirect.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Reads a port number for a connection.
|
|
6
|
+
*
|
|
7
|
+
* @param port Existing information about the port.
|
|
8
|
+
* @param protocol Request protocol. Only used if `port` is not set.
|
|
9
|
+
* @return A port number. Default to 80.
|
|
10
|
+
*/
|
|
11
|
+
export function getPort(port: number|string, protocol?: string): number {
|
|
12
|
+
if (port) {
|
|
13
|
+
const typedPort = Number(port);
|
|
14
|
+
if (!Number.isNaN(typedPort)) {
|
|
15
|
+
return typedPort;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
if (protocol === 'https:') {
|
|
19
|
+
return 443;
|
|
20
|
+
}
|
|
21
|
+
return 80;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Creates a value for host header.
|
|
26
|
+
*
|
|
27
|
+
* @param value An url to get the information from.
|
|
28
|
+
* @return Value of the host header
|
|
29
|
+
*/
|
|
30
|
+
export function getHostHeader(value: string): string | undefined {
|
|
31
|
+
let uri;
|
|
32
|
+
try {
|
|
33
|
+
uri = new URL(value);
|
|
34
|
+
} catch (e) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
let hostValue = uri.hostname;
|
|
38
|
+
const defaultPorts = [80, 443];
|
|
39
|
+
const port = getPort(uri.port, uri.protocol);
|
|
40
|
+
if (!defaultPorts.includes(port)) {
|
|
41
|
+
hostValue += `:${port}`;
|
|
42
|
+
}
|
|
43
|
+
return hostValue;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Adds the `content-length` header to current request headers list if
|
|
48
|
+
* it's required.
|
|
49
|
+
* This function will do nothing if the request do not carry a payload or
|
|
50
|
+
* when the content length header is already set.
|
|
51
|
+
*
|
|
52
|
+
* @param method HTTP request method
|
|
53
|
+
* @param buffer Generated message buffer.
|
|
54
|
+
* @param headers A headers object where to append headers when needed
|
|
55
|
+
*/
|
|
56
|
+
export function addContentLength(method: string, buffer: Buffer, headers: Headers): void {
|
|
57
|
+
if (method.toLowerCase() === 'get') {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const size = buffer ? buffer.length : 0;
|
|
61
|
+
headers.set('content-length', String(size));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export declare interface RedirectOptions {
|
|
65
|
+
/**
|
|
66
|
+
* true if redirect is required
|
|
67
|
+
*/
|
|
68
|
+
redirect?: boolean;
|
|
69
|
+
/**
|
|
70
|
+
* If true the redirected request has to be a GET request.
|
|
71
|
+
*/
|
|
72
|
+
forceGet?: boolean;
|
|
73
|
+
/**
|
|
74
|
+
* location of the resource (redirect uri)
|
|
75
|
+
*/
|
|
76
|
+
location?: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Checks if redirect is required.
|
|
81
|
+
* @param status Response status code
|
|
82
|
+
* @param method Request HTTP method
|
|
83
|
+
* @param location Location header value, if any
|
|
84
|
+
* @returns The redirect options
|
|
85
|
+
*/
|
|
86
|
+
export function redirectOptions(status: number, method: string, location?: string): RedirectOptions {
|
|
87
|
+
const result: RedirectOptions = {
|
|
88
|
+
redirect: false,
|
|
89
|
+
forceGet: false,
|
|
90
|
+
};
|
|
91
|
+
switch (status) {
|
|
92
|
+
case 300:
|
|
93
|
+
case 304:
|
|
94
|
+
case 305:
|
|
95
|
+
// do nothing;
|
|
96
|
+
break;
|
|
97
|
+
case 301:
|
|
98
|
+
case 302:
|
|
99
|
+
case 307:
|
|
100
|
+
if (['GET', 'HEAD'].indexOf(method) !== -1) {
|
|
101
|
+
result.redirect = true;
|
|
102
|
+
}
|
|
103
|
+
break;
|
|
104
|
+
case 303:
|
|
105
|
+
result.redirect = true;
|
|
106
|
+
result.forceGet = true;
|
|
107
|
+
break;
|
|
108
|
+
default:
|
|
109
|
+
}
|
|
110
|
+
if (!result.redirect) {
|
|
111
|
+
return result;
|
|
112
|
+
}
|
|
113
|
+
if (location) {
|
|
114
|
+
result.location = location;
|
|
115
|
+
}
|
|
116
|
+
return result;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Checks if request is an infinite loop.
|
|
122
|
+
* @param location Redirect location
|
|
123
|
+
* @param redirects List of response objects
|
|
124
|
+
* @return True if redirect is into the same place as already visited.
|
|
125
|
+
*/
|
|
126
|
+
export function isRedirectLoop(location: string, redirects: ResponseRedirect[]): boolean {
|
|
127
|
+
if (redirects) {
|
|
128
|
+
let index = -1;
|
|
129
|
+
let i = 0;
|
|
130
|
+
for (const item of redirects) {
|
|
131
|
+
if (item.url === location) {
|
|
132
|
+
index = i;
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
i++;
|
|
136
|
+
}
|
|
137
|
+
if (index !== -1) {
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Processes redirection location
|
|
146
|
+
*
|
|
147
|
+
* @param location Redirect location
|
|
148
|
+
* @param requestUrl Request url
|
|
149
|
+
* @return Redirect location
|
|
150
|
+
*/
|
|
151
|
+
export function getRedirectLocation(location: string, requestUrl: string): string | undefined {
|
|
152
|
+
// https://github.com/jarrodek/socket-fetch/issues/5
|
|
153
|
+
try {
|
|
154
|
+
// eslint-disable-next-line no-new
|
|
155
|
+
new URL(location);
|
|
156
|
+
} catch (e) {
|
|
157
|
+
try {
|
|
158
|
+
location = new URL(location, requestUrl).toString();
|
|
159
|
+
} catch (_) {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return location;
|
|
164
|
+
}
|
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
// Paul Tero, July 2001
|
|
2
|
+
// http://www.tero.co.uk/des/
|
|
3
|
+
//
|
|
4
|
+
// Optimised for performance with large blocks by Michael Hayworth, November 2001
|
|
5
|
+
// http://www.netdealing.com
|
|
6
|
+
//
|
|
7
|
+
// THIS SOFTWARE IS PROVIDED "AS IS" AND
|
|
8
|
+
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
9
|
+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
10
|
+
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
11
|
+
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
12
|
+
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
13
|
+
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
14
|
+
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
15
|
+
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
16
|
+
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
17
|
+
// SUCH DAMAGE.
|
|
18
|
+
export class Des {
|
|
19
|
+
static des(key: string, message: string, encrypt: number, mode: number, iv?: string, padding?: number): string {
|
|
20
|
+
const spfunction1 = [0x1010400, 0, 0x10000, 0x1010404, 0x1010004, 0x10404,
|
|
21
|
+
0x4, 0x10000, 0x400, 0x1010400, 0x1010404, 0x400, 0x1000404, 0x1010004,
|
|
22
|
+
0x1000000, 0x4, 0x404, 0x1000400, 0x1000400, 0x10400, 0x10400, 0x1010000,
|
|
23
|
+
0x1010000, 0x1000404, 0x10004, 0x1000004, 0x1000004, 0x10004, 0, 0x404,
|
|
24
|
+
0x10404, 0x1000000, 0x10000, 0x1010404, 0x4, 0x1010000, 0x1010400, 0x1000000,
|
|
25
|
+
0x1000000, 0x400, 0x1010004, 0x10000, 0x10400, 0x1000004, 0x400, 0x4, 0x1000404,
|
|
26
|
+
0x10404, 0x1010404, 0x10004, 0x1010000, 0x1000404, 0x1000004, 0x404, 0x10404,
|
|
27
|
+
0x1010400, 0x404, 0x1000400, 0x1000400, 0, 0x10004, 0x10400, 0, 0x1010004,
|
|
28
|
+
];
|
|
29
|
+
const spfunction2 = [-0x7fef7fe0, -0x7fff8000, 0x8000, 0x108020, 0x100000, 0x20,
|
|
30
|
+
-0x7fefffe0, -0x7fff7fe0, -0x7fffffe0, -0x7fef7fe0, -0x7fef8000, -0x80000000,
|
|
31
|
+
-0x7fff8000, 0x100000, 0x20, -0x7fefffe0, 0x108000, 0x100020, -0x7fff7fe0, 0,
|
|
32
|
+
-0x80000000, 0x8000, 0x108020, -0x7ff00000, 0x100020, -0x7fffffe0, 0, 0x108000,
|
|
33
|
+
0x8020, -0x7fef8000, -0x7ff00000, 0x8020, 0, 0x108020, -0x7fefffe0, 0x100000,
|
|
34
|
+
-0x7fff7fe0, -0x7ff00000, -0x7fef8000, 0x8000, -0x7ff00000, -0x7fff8000, 0x20,
|
|
35
|
+
-0x7fef7fe0, 0x108020, 0x20, 0x8000, -0x80000000, 0x8020, -0x7fef8000, 0x100000,
|
|
36
|
+
-0x7fffffe0, 0x100020, -0x7fff7fe0, -0x7fffffe0, 0x100020, 0x108000, 0, -0x7fff8000,
|
|
37
|
+
0x8020, -0x80000000, -0x7fefffe0, -0x7fef7fe0, 0x108000,
|
|
38
|
+
];
|
|
39
|
+
const spfunction3 = [0x208, 0x8020200, 0, 0x8020008, 0x8000200, 0, 0x20208, 0x8000200,
|
|
40
|
+
0x20008, 0x8000008, 0x8000008, 0x20000, 0x8020208, 0x20008, 0x8020000, 0x208,
|
|
41
|
+
0x8000000, 0x8, 0x8020200, 0x200, 0x20200, 0x8020000, 0x8020008, 0x20208,
|
|
42
|
+
0x8000208, 0x20200, 0x20000, 0x8000208, 0x8, 0x8020208, 0x200, 0x8000000,
|
|
43
|
+
0x8020200, 0x8000000, 0x20008, 0x208, 0x20000, 0x8020200, 0x8000200, 0, 0x200,
|
|
44
|
+
0x20008, 0x8020208, 0x8000200, 0x8000008, 0x200, 0, 0x8020008, 0x8000208, 0x20000,
|
|
45
|
+
0x8000000, 0x8020208, 0x8, 0x20208, 0x20200, 0x8000008, 0x8020000, 0x8000208,
|
|
46
|
+
0x208, 0x8020000, 0x20208, 0x8, 0x8020008, 0x20200,
|
|
47
|
+
];
|
|
48
|
+
const spfunction4 = [0x802001, 0x2081, 0x2081, 0x80, 0x802080, 0x800081, 0x800001,
|
|
49
|
+
0x2001, 0, 0x802000, 0x802000, 0x802081, 0x81, 0, 0x800080, 0x800001, 0x1,
|
|
50
|
+
0x2000, 0x800000, 0x802001, 0x80, 0x800000, 0x2001, 0x2080, 0x800081, 0x1, 0x2080,
|
|
51
|
+
0x800080, 0x2000, 0x802080, 0x802081, 0x81, 0x800080, 0x800001, 0x802000, 0x802081,
|
|
52
|
+
0x81, 0, 0, 0x802000, 0x2080, 0x800080, 0x800081, 0x1, 0x802001, 0x2081, 0x2081,
|
|
53
|
+
0x80, 0x802081, 0x81, 0x1, 0x2000, 0x800001, 0x2001, 0x802080, 0x800081, 0x2001,
|
|
54
|
+
0x2080, 0x800000, 0x802001, 0x80, 0x800000, 0x2000, 0x802080,
|
|
55
|
+
];
|
|
56
|
+
const spfunction5 = [0x100, 0x2080100, 0x2080000, 0x42000100, 0x80000, 0x100, 0x40000000,
|
|
57
|
+
0x2080000, 0x40080100, 0x80000, 0x2000100, 0x40080100, 0x42000100, 0x42080000, 0x80100,
|
|
58
|
+
0x40000000, 0x2000000, 0x40080000, 0x40080000, 0, 0x40000100, 0x42080100, 0x42080100,
|
|
59
|
+
0x2000100, 0x42080000, 0x40000100, 0, 0x42000000, 0x2080100, 0x2000000, 0x42000000,
|
|
60
|
+
0x80100, 0x80000, 0x42000100, 0x100, 0x2000000, 0x40000000, 0x2080000, 0x42000100,
|
|
61
|
+
0x40080100, 0x2000100, 0x40000000, 0x42080000, 0x2080100, 0x40080100, 0x100, 0x2000000,
|
|
62
|
+
0x42080000, 0x42080100, 0x80100, 0x42000000, 0x42080100, 0x2080000, 0, 0x40080000,
|
|
63
|
+
0x42000000, 0x80100, 0x2000100, 0x40000100, 0x80000, 0, 0x40080000, 0x2080100,
|
|
64
|
+
0x40000100,
|
|
65
|
+
];
|
|
66
|
+
const spfunction6 = [0x20000010, 0x20400000, 0x4000, 0x20404010, 0x20400000, 0x10,
|
|
67
|
+
0x20404010, 0x400000, 0x20004000, 0x404010, 0x400000, 0x20000010, 0x400010, 0x20004000,
|
|
68
|
+
0x20000000, 0x4010, 0, 0x400010, 0x20004010, 0x4000, 0x404000, 0x20004010, 0x10,
|
|
69
|
+
0x20400010, 0x20400010, 0, 0x404010, 0x20404000, 0x4010, 0x404000, 0x20404000,
|
|
70
|
+
0x20000000, 0x20004000, 0x10, 0x20400010, 0x404000, 0x20404010, 0x400000, 0x4010,
|
|
71
|
+
0x20000010, 0x400000, 0x20004000, 0x20000000, 0x4010, 0x20000010, 0x20404010, 0x404000,
|
|
72
|
+
0x20400000, 0x404010, 0x20404000, 0, 0x20400010, 0x10, 0x4000, 0x20400000, 0x404010,
|
|
73
|
+
0x4000, 0x400010, 0x20004010, 0, 0x20404000, 0x20000000, 0x400010, 0x20004010,
|
|
74
|
+
];
|
|
75
|
+
const spfunction7 = [0x200000, 0x4200002, 0x4000802, 0, 0x800, 0x4000802, 0x200802,
|
|
76
|
+
0x4200800, 0x4200802, 0x200000, 0, 0x4000002, 0x2, 0x4000000, 0x4200002, 0x802,
|
|
77
|
+
0x4000800, 0x200802, 0x200002, 0x4000800, 0x4000002, 0x4200000, 0x4200800, 0x200002,
|
|
78
|
+
0x4200000, 0x800, 0x802, 0x4200802, 0x200800, 0x2, 0x4000000, 0x200800, 0x4000000,
|
|
79
|
+
0x200800, 0x200000, 0x4000802, 0x4000802, 0x4200002, 0x4200002, 0x2, 0x200002,
|
|
80
|
+
0x4000000, 0x4000800, 0x200000, 0x4200800, 0x802, 0x200802, 0x4200800, 0x802,
|
|
81
|
+
0x4000002, 0x4200802, 0x4200000, 0x200800, 0, 0x2, 0x4200802, 0, 0x200802, 0x4200000,
|
|
82
|
+
0x800, 0x4000002, 0x4000800, 0x800, 0x200002,
|
|
83
|
+
];
|
|
84
|
+
const spfunction8 = [0x10001040, 0x1000, 0x40000, 0x10041040, 0x10000000, 0x10001040,
|
|
85
|
+
0x40, 0x10000000, 0x40040, 0x10040000, 0x10041040, 0x41000, 0x10041000, 0x41040,
|
|
86
|
+
0x1000, 0x40, 0x10040000, 0x10000040, 0x10001000, 0x1040, 0x41000, 0x40040, 0x10040040,
|
|
87
|
+
0x10041000, 0x1040, 0, 0, 0x10040040, 0x10000040, 0x10001000, 0x41040, 0x40000, 0x41040,
|
|
88
|
+
0x40000, 0x10041000, 0x1000, 0x40, 0x10040040, 0x1000, 0x41040, 0x10001000, 0x40,
|
|
89
|
+
0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x40000, 0x10001040, 0, 0x10041040,
|
|
90
|
+
0x40040, 0x10000040, 0x10040000, 0x10001000, 0x10001040, 0, 0x10041040, 0x41000,
|
|
91
|
+
0x41000, 0x1040, 0x1040, 0x40040, 0x10000000, 0x10041000,
|
|
92
|
+
];
|
|
93
|
+
|
|
94
|
+
// create the 16 or 48 subkeys we will need
|
|
95
|
+
const keys = Des.createKeys(key);
|
|
96
|
+
let m = 0;
|
|
97
|
+
let i;
|
|
98
|
+
let j;
|
|
99
|
+
let temp;
|
|
100
|
+
let right1;
|
|
101
|
+
let right2;
|
|
102
|
+
let left;
|
|
103
|
+
let right;
|
|
104
|
+
let looping;
|
|
105
|
+
let cbcleft;
|
|
106
|
+
let cbcleft2;
|
|
107
|
+
let cbcright;
|
|
108
|
+
let cbcright2;
|
|
109
|
+
let endloop;
|
|
110
|
+
let loopinc;
|
|
111
|
+
let len = message.length;
|
|
112
|
+
let chunk = 0;
|
|
113
|
+
const iterations = keys.length === 32 ? 3 : 9;
|
|
114
|
+
if (iterations === 3) {
|
|
115
|
+
looping = encrypt ? [0, 32, 2] : [30, -2, -2];
|
|
116
|
+
} else {
|
|
117
|
+
looping = encrypt ? [0, 32, 2, 62, 30, -2, 64, 96, 2] : [94, 62, -2, 32, 64, 2, 30, -2, -2];
|
|
118
|
+
}
|
|
119
|
+
if (padding === 2) {
|
|
120
|
+
message += ' ';
|
|
121
|
+
} else if (padding === 1) {
|
|
122
|
+
temp = 8 - (len % 8);
|
|
123
|
+
message += String.fromCharCode(temp, temp, temp, temp, temp, temp, temp, temp);
|
|
124
|
+
if (temp === 8) {
|
|
125
|
+
len += 8;
|
|
126
|
+
}
|
|
127
|
+
} else if (!padding) {
|
|
128
|
+
message += '\0\0\0\0\0\0\0\0';
|
|
129
|
+
}
|
|
130
|
+
let result = '';
|
|
131
|
+
let tempresult = '';
|
|
132
|
+
if (mode === 1) {
|
|
133
|
+
cbcleft = (iv!.charCodeAt(m++) << 24) | (iv!.charCodeAt(m++) << 16) |
|
|
134
|
+
(iv!.charCodeAt(m++) << 8) | iv!.charCodeAt(m++);
|
|
135
|
+
cbcright = (iv!.charCodeAt(m++) << 24) | (iv!.charCodeAt(m++) << 16) |
|
|
136
|
+
(iv!.charCodeAt(m++) << 8) | iv!.charCodeAt(m++);
|
|
137
|
+
m = 0;
|
|
138
|
+
}
|
|
139
|
+
while (m < len) {
|
|
140
|
+
left = (message.charCodeAt(m++) << 24) | (message.charCodeAt(m++) << 16) |
|
|
141
|
+
(message.charCodeAt(m++) << 8) | message.charCodeAt(m++);
|
|
142
|
+
right = (message.charCodeAt(m++) << 24) | (message.charCodeAt(m++) << 16) |
|
|
143
|
+
(message.charCodeAt(m++) << 8) | message.charCodeAt(m++);
|
|
144
|
+
if (mode === 1) {
|
|
145
|
+
if (encrypt) {
|
|
146
|
+
left ^= cbcleft as number;
|
|
147
|
+
right ^= cbcright as number;
|
|
148
|
+
} else {
|
|
149
|
+
cbcleft2 = cbcleft;
|
|
150
|
+
cbcright2 = cbcright;
|
|
151
|
+
cbcleft = left;
|
|
152
|
+
cbcright = right;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
temp = ((left >>> 4) ^ right) & 0x0f0f0f0f;
|
|
156
|
+
right ^= temp;
|
|
157
|
+
left ^= (temp << 4);
|
|
158
|
+
temp = ((left >>> 16) ^ right) & 0x0000ffff;
|
|
159
|
+
right ^= temp;
|
|
160
|
+
left ^= (temp << 16);
|
|
161
|
+
temp = ((right >>> 2) ^ left) & 0x33333333;
|
|
162
|
+
left ^= temp;
|
|
163
|
+
right ^= (temp << 2);
|
|
164
|
+
temp = ((right >>> 8) ^ left) & 0x00ff00ff;
|
|
165
|
+
left ^= temp;
|
|
166
|
+
right ^= (temp << 8);
|
|
167
|
+
temp = ((left >>> 1) ^ right) & 0x55555555;
|
|
168
|
+
right ^= temp;
|
|
169
|
+
left ^= (temp << 1);
|
|
170
|
+
|
|
171
|
+
left = ((left << 1) | (left >>> 31));
|
|
172
|
+
right = ((right << 1) | (right >>> 31));
|
|
173
|
+
for (j = 0; j < iterations; j += 3) {
|
|
174
|
+
endloop = looping[j + 1];
|
|
175
|
+
loopinc = looping[j + 2];
|
|
176
|
+
for (i = looping[j]; i !== endloop; i += loopinc) {
|
|
177
|
+
right1 = right ^ keys[i];
|
|
178
|
+
right2 = ((right >>> 4) | (right << 28)) ^ keys[i + 1];
|
|
179
|
+
temp = left;
|
|
180
|
+
left = right;
|
|
181
|
+
right = temp ^ (spfunction2[(right1 >>> 24) & 0x3f] |
|
|
182
|
+
spfunction4[(right1 >>> 16) & 0x3f] | spfunction6[(right1 >>> 8) & 0x3f] |
|
|
183
|
+
spfunction8[right1 & 0x3f] | spfunction1[(right2 >>> 24) & 0x3f] |
|
|
184
|
+
spfunction3[(right2 >>> 16) & 0x3f] | spfunction5[(right2 >>> 8) & 0x3f] |
|
|
185
|
+
spfunction7[right2 & 0x3f]);
|
|
186
|
+
}
|
|
187
|
+
temp = left;
|
|
188
|
+
left = right;
|
|
189
|
+
right = temp;
|
|
190
|
+
}
|
|
191
|
+
left = ((left >>> 1) | (left << 31));
|
|
192
|
+
right = ((right >>> 1) | (right << 31));
|
|
193
|
+
temp = ((left >>> 1) ^ right) & 0x55555555;
|
|
194
|
+
right ^= temp;
|
|
195
|
+
left ^= (temp << 1);
|
|
196
|
+
temp = ((right >>> 8) ^ left) & 0x00ff00ff;
|
|
197
|
+
left ^= temp;
|
|
198
|
+
right ^= (temp << 8);
|
|
199
|
+
temp = ((right >>> 2) ^ left) & 0x33333333;
|
|
200
|
+
left ^= temp;
|
|
201
|
+
right ^= (temp << 2);
|
|
202
|
+
temp = ((left >>> 16) ^ right) & 0x0000ffff;
|
|
203
|
+
right ^= temp;
|
|
204
|
+
left ^= (temp << 16);
|
|
205
|
+
temp = ((left >>> 4) ^ right) & 0x0f0f0f0f;
|
|
206
|
+
right ^= temp;
|
|
207
|
+
left ^= (temp << 4);
|
|
208
|
+
if (mode === 1) {
|
|
209
|
+
if (encrypt) {
|
|
210
|
+
cbcleft = left;
|
|
211
|
+
cbcright = right;
|
|
212
|
+
} else {
|
|
213
|
+
left ^= cbcleft2 as number;
|
|
214
|
+
right ^= cbcright2 as number;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
tempresult += String.fromCharCode((left >>> 24), ((left >>> 16) & 0xff),
|
|
218
|
+
((left >>> 8) & 0xff), (left & 0xff), (right >>> 24),
|
|
219
|
+
((right >>> 16) & 0xff), ((right >>> 8) & 0xff), (right & 0xff));
|
|
220
|
+
|
|
221
|
+
chunk += 8;
|
|
222
|
+
if (chunk === 512) {
|
|
223
|
+
result += tempresult;
|
|
224
|
+
tempresult = '';
|
|
225
|
+
chunk = 0;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return result + tempresult;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
static createKeys(key: string): number[] {
|
|
232
|
+
const pc2bytes0 = [0, 0x4, 0x20000000, 0x20000004, 0x10000, 0x10004, 0x20010000,
|
|
233
|
+
0x20010004, 0x200, 0x204, 0x20000200, 0x20000204, 0x10200, 0x10204, 0x20010200,
|
|
234
|
+
0x20010204,
|
|
235
|
+
];
|
|
236
|
+
const pc2bytes1 = [0, 0x1, 0x100000, 0x100001, 0x4000000, 0x4000001, 0x4100000,
|
|
237
|
+
0x4100001, 0x100, 0x101, 0x100100, 0x100101, 0x4000100, 0x4000101, 0x4100100,
|
|
238
|
+
0x4100101,
|
|
239
|
+
];
|
|
240
|
+
const pc2bytes2 = [0, 0x8, 0x800, 0x808, 0x1000000, 0x1000008, 0x1000800, 0x1000808,
|
|
241
|
+
0, 0x8, 0x800, 0x808, 0x1000000, 0x1000008, 0x1000800, 0x1000808,
|
|
242
|
+
];
|
|
243
|
+
const pc2bytes3 = [0, 0x200000, 0x8000000, 0x8200000, 0x2000, 0x202000, 0x8002000,
|
|
244
|
+
0x8202000, 0x20000, 0x220000, 0x8020000, 0x8220000, 0x22000, 0x222000, 0x8022000,
|
|
245
|
+
0x8222000,
|
|
246
|
+
];
|
|
247
|
+
const pc2bytes4 = [0, 0x40000, 0x10, 0x40010, 0, 0x40000, 0x10, 0x40010, 0x1000,
|
|
248
|
+
0x41000, 0x1010, 0x41010, 0x1000, 0x41000, 0x1010, 0x41010,
|
|
249
|
+
];
|
|
250
|
+
const pc2bytes5 = [0, 0x400, 0x20, 0x420, 0, 0x400, 0x20, 0x420, 0x2000000, 0x2000400,
|
|
251
|
+
0x2000020, 0x2000420, 0x2000000, 0x2000400, 0x2000020, 0x2000420,
|
|
252
|
+
];
|
|
253
|
+
const pc2bytes6 = [0, 0x10000000, 0x80000, 0x10080000, 0x2, 0x10000002, 0x80002,
|
|
254
|
+
0x10080002, 0, 0x10000000, 0x80000, 0x10080000, 0x2, 0x10000002, 0x80002, 0x10080002,
|
|
255
|
+
];
|
|
256
|
+
const pc2bytes7 = [0, 0x10000, 0x800, 0x10800, 0x20000000, 0x20010000, 0x20000800,
|
|
257
|
+
0x20010800, 0x20000, 0x30000, 0x20800, 0x30800, 0x20020000, 0x20030000, 0x20020800,
|
|
258
|
+
0x20030800,
|
|
259
|
+
];
|
|
260
|
+
const pc2bytes8 = [0, 0x40000, 0, 0x40000, 0x2, 0x40002, 0x2, 0x40002, 0x2000000,
|
|
261
|
+
0x2040000, 0x2000000, 0x2040000, 0x2000002, 0x2040002, 0x2000002, 0x2040002,
|
|
262
|
+
];
|
|
263
|
+
const pc2bytes9 = [0, 0x10000000, 0x8, 0x10000008, 0, 0x10000000, 0x8, 0x10000008,
|
|
264
|
+
0x400, 0x10000400, 0x408, 0x10000408, 0x400, 0x10000400, 0x408, 0x10000408,
|
|
265
|
+
];
|
|
266
|
+
const pc2bytes10 = [0, 0x20, 0, 0x20, 0x100000, 0x100020, 0x100000, 0x100020, 0x2000,
|
|
267
|
+
0x2020, 0x2000, 0x2020, 0x102000, 0x102020, 0x102000, 0x102020,
|
|
268
|
+
];
|
|
269
|
+
const pc2bytes11 = [0, 0x1000000, 0x200, 0x1000200, 0x200000, 0x1200000, 0x200200,
|
|
270
|
+
0x1200200, 0x4000000, 0x5000000, 0x4000200, 0x5000200, 0x4200000, 0x5200000,
|
|
271
|
+
0x4200200, 0x5200200,
|
|
272
|
+
];
|
|
273
|
+
const pc2bytes12 = [0, 0x1000, 0x8000000, 0x8001000, 0x80000, 0x81000, 0x8080000,
|
|
274
|
+
0x8081000, 0x10, 0x1010, 0x8000010, 0x8001010, 0x80010, 0x81010, 0x8080010, 0x8081010,
|
|
275
|
+
];
|
|
276
|
+
const pc2bytes13 = [0, 0x4, 0x100, 0x104, 0, 0x4, 0x100, 0x104, 0x1, 0x5, 0x101,
|
|
277
|
+
0x105, 0x1, 0x5, 0x101, 0x105,
|
|
278
|
+
];
|
|
279
|
+
|
|
280
|
+
const iterations = key.length > 8 ? 3 : 1;
|
|
281
|
+
const keys: number[] = new Array(32 * iterations);
|
|
282
|
+
const shifts = [0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0];
|
|
283
|
+
let lefttemp;
|
|
284
|
+
let righttemp;
|
|
285
|
+
let m = 0;
|
|
286
|
+
let n = 0;
|
|
287
|
+
let temp;
|
|
288
|
+
|
|
289
|
+
for (let j = 0; j < iterations; j++) {
|
|
290
|
+
let left = (key.charCodeAt(m++) << 24) |
|
|
291
|
+
(key.charCodeAt(m++) << 16) | (key.charCodeAt(m++) << 8) | key.charCodeAt(m++);
|
|
292
|
+
let right = (key.charCodeAt(m++) << 24) | (key.charCodeAt(m++) << 16) |
|
|
293
|
+
(key.charCodeAt(m++) << 8) | key.charCodeAt(m++);
|
|
294
|
+
|
|
295
|
+
temp = ((left >>> 4) ^ right) & 0x0f0f0f0f;
|
|
296
|
+
right ^= temp;
|
|
297
|
+
left ^= (temp << 4);
|
|
298
|
+
temp = ((right >>> -16) ^ left) & 0x0000ffff;
|
|
299
|
+
left ^= temp;
|
|
300
|
+
right ^= (temp << -16);
|
|
301
|
+
temp = ((left >>> 2) ^ right) & 0x33333333;
|
|
302
|
+
right ^= temp;
|
|
303
|
+
left ^= (temp << 2);
|
|
304
|
+
temp = ((right >>> -16) ^ left) & 0x0000ffff;
|
|
305
|
+
left ^= temp;
|
|
306
|
+
right ^= (temp << -16);
|
|
307
|
+
temp = ((left >>> 1) ^ right) & 0x55555555;
|
|
308
|
+
right ^= temp;
|
|
309
|
+
left ^= (temp << 1);
|
|
310
|
+
temp = ((right >>> 8) ^ left) & 0x00ff00ff;
|
|
311
|
+
left ^= temp;
|
|
312
|
+
right ^= (temp << 8);
|
|
313
|
+
temp = ((left >>> 1) ^ right) & 0x55555555;
|
|
314
|
+
right ^= temp;
|
|
315
|
+
left ^= (temp << 1);
|
|
316
|
+
temp = (left << 8) | ((right >>> 20) & 0x000000f0);
|
|
317
|
+
left = (right << 24) | ((right << 8) & 0xff0000) |
|
|
318
|
+
((right >>> 8) & 0xff00) | ((right >>> 24) & 0xf0);
|
|
319
|
+
right = temp;
|
|
320
|
+
for (let i = 0; i < shifts.length; i++) {
|
|
321
|
+
if (shifts[i]) {
|
|
322
|
+
left = (left << 2) | (left >>> 26);
|
|
323
|
+
right = (right << 2) | (right >>> 26);
|
|
324
|
+
} else {
|
|
325
|
+
left = (left << 1) | (left >>> 27);
|
|
326
|
+
right = (right << 1) | (right >>> 27);
|
|
327
|
+
}
|
|
328
|
+
left &= -0xf;
|
|
329
|
+
right &= -0xf;
|
|
330
|
+
lefttemp = pc2bytes0[left >>> 28] | pc2bytes1[(left >>> 24) & 0xf] |
|
|
331
|
+
pc2bytes2[(left >>> 20) & 0xf] | pc2bytes3[(left >>> 16) & 0xf] |
|
|
332
|
+
pc2bytes4[(left >>> 12) & 0xf] | pc2bytes5[(left >>> 8) & 0xf] |
|
|
333
|
+
pc2bytes6[(left >>> 4) & 0xf];
|
|
334
|
+
righttemp = pc2bytes7[right >>> 28] | pc2bytes8[(right >>> 24) & 0xf] |
|
|
335
|
+
pc2bytes9[(right >>> 20) & 0xf] | pc2bytes10[(right >>> 16) & 0xf] |
|
|
336
|
+
pc2bytes11[(right >>> 12) & 0xf] | pc2bytes12[(right >>> 8) & 0xf] |
|
|
337
|
+
pc2bytes13[(right >>> 4) & 0xf];
|
|
338
|
+
temp = ((righttemp >>> 16) ^ lefttemp) & 0x0000ffff;
|
|
339
|
+
keys[n++] = lefttemp ^ temp;
|
|
340
|
+
keys[n++] = righttemp ^ (temp << 16);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
return keys;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* A JavaScript implementation of the RSA Data Security, Inc. MD4 Message
|
|
3
|
+
* Digest Algorithm, as defined in RFC 1320.
|
|
4
|
+
* Version 2.1 Copyright (C) Jerrad Pierce, Paul Johnston 1999 - 2002.
|
|
5
|
+
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
|
|
6
|
+
* Distributed under the BSD License
|
|
7
|
+
* See http://pajhome.org.uk/crypt/md5 for more info.
|
|
8
|
+
*/
|
|
9
|
+
export class MD4 {
|
|
10
|
+
static str(s: string): string {
|
|
11
|
+
return MD4.binl2str(MD4.core(MD4.str2binl(s), s.length * MD4.chrsz));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
static core(x: any[], len: number): number[] {
|
|
15
|
+
x[len >> 5] |= 0x80 << (len % 32);
|
|
16
|
+
x[(((len + 64) >>> 9) << 4) + 14] = len;
|
|
17
|
+
|
|
18
|
+
let a = 1732584193;
|
|
19
|
+
let b = -271733879;
|
|
20
|
+
let c = -1732584194;
|
|
21
|
+
let d = 271733878;
|
|
22
|
+
|
|
23
|
+
for (let i = 0; i < x.length; i += 16) {
|
|
24
|
+
const olda = a;
|
|
25
|
+
const oldb = b;
|
|
26
|
+
const oldc = c;
|
|
27
|
+
const oldd = d;
|
|
28
|
+
|
|
29
|
+
a = MD4.ff(a, b, c, d, x[i + 0], 3);
|
|
30
|
+
d = MD4.ff(d, a, b, c, x[i + 1], 7);
|
|
31
|
+
c = MD4.ff(c, d, a, b, x[i + 2], 11);
|
|
32
|
+
b = MD4.ff(b, c, d, a, x[i + 3], 19);
|
|
33
|
+
a = MD4.ff(a, b, c, d, x[i + 4], 3);
|
|
34
|
+
d = MD4.ff(d, a, b, c, x[i + 5], 7);
|
|
35
|
+
c = MD4.ff(c, d, a, b, x[i + 6], 11);
|
|
36
|
+
b = MD4.ff(b, c, d, a, x[i + 7], 19);
|
|
37
|
+
a = MD4.ff(a, b, c, d, x[i + 8], 3);
|
|
38
|
+
d = MD4.ff(d, a, b, c, x[i + 9], 7);
|
|
39
|
+
c = MD4.ff(c, d, a, b, x[i + 10], 11);
|
|
40
|
+
b = MD4.ff(b, c, d, a, x[i + 11], 19);
|
|
41
|
+
a = MD4.ff(a, b, c, d, x[i + 12], 3);
|
|
42
|
+
d = MD4.ff(d, a, b, c, x[i + 13], 7);
|
|
43
|
+
c = MD4.ff(c, d, a, b, x[i + 14], 11);
|
|
44
|
+
b = MD4.ff(b, c, d, a, x[i + 15], 19);
|
|
45
|
+
|
|
46
|
+
a = MD4.gg(a, b, c, d, x[i + 0], 3);
|
|
47
|
+
d = MD4.gg(d, a, b, c, x[i + 4], 5);
|
|
48
|
+
c = MD4.gg(c, d, a, b, x[i + 8], 9);
|
|
49
|
+
b = MD4.gg(b, c, d, a, x[i + 12], 13);
|
|
50
|
+
a = MD4.gg(a, b, c, d, x[i + 1], 3);
|
|
51
|
+
d = MD4.gg(d, a, b, c, x[i + 5], 5);
|
|
52
|
+
c = MD4.gg(c, d, a, b, x[i + 9], 9);
|
|
53
|
+
b = MD4.gg(b, c, d, a, x[i + 13], 13);
|
|
54
|
+
a = MD4.gg(a, b, c, d, x[i + 2], 3);
|
|
55
|
+
d = MD4.gg(d, a, b, c, x[i + 6], 5);
|
|
56
|
+
c = MD4.gg(c, d, a, b, x[i + 10], 9);
|
|
57
|
+
b = MD4.gg(b, c, d, a, x[i + 14], 13);
|
|
58
|
+
a = MD4.gg(a, b, c, d, x[i + 3], 3);
|
|
59
|
+
d = MD4.gg(d, a, b, c, x[i + 7], 5);
|
|
60
|
+
c = MD4.gg(c, d, a, b, x[i + 11], 9);
|
|
61
|
+
b = MD4.gg(b, c, d, a, x[i + 15], 13);
|
|
62
|
+
|
|
63
|
+
a = MD4.hh(a, b, c, d, x[i + 0], 3);
|
|
64
|
+
d = MD4.hh(d, a, b, c, x[i + 8], 9);
|
|
65
|
+
c = MD4.hh(c, d, a, b, x[i + 4], 11);
|
|
66
|
+
b = MD4.hh(b, c, d, a, x[i + 12], 15);
|
|
67
|
+
a = MD4.hh(a, b, c, d, x[i + 2], 3);
|
|
68
|
+
d = MD4.hh(d, a, b, c, x[i + 10], 9);
|
|
69
|
+
c = MD4.hh(c, d, a, b, x[i + 6], 11);
|
|
70
|
+
b = MD4.hh(b, c, d, a, x[i + 14], 15);
|
|
71
|
+
a = MD4.hh(a, b, c, d, x[i + 1], 3);
|
|
72
|
+
d = MD4.hh(d, a, b, c, x[i + 9], 9);
|
|
73
|
+
c = MD4.hh(c, d, a, b, x[i + 5], 11);
|
|
74
|
+
b = MD4.hh(b, c, d, a, x[i + 13], 15);
|
|
75
|
+
a = MD4.hh(a, b, c, d, x[i + 3], 3);
|
|
76
|
+
d = MD4.hh(d, a, b, c, x[i + 11], 9);
|
|
77
|
+
c = MD4.hh(c, d, a, b, x[i + 7], 11);
|
|
78
|
+
b = MD4.hh(b, c, d, a, x[i + 15], 15);
|
|
79
|
+
|
|
80
|
+
a = MD4.safeAdd(a, olda);
|
|
81
|
+
b = MD4.safeAdd(b, oldb);
|
|
82
|
+
c = MD4.safeAdd(c, oldc);
|
|
83
|
+
d = MD4.safeAdd(d, oldd);
|
|
84
|
+
}
|
|
85
|
+
return [a, b, c, d];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
static cmn(q: number, a: number, b: number, x: number, s: number, t: number): number {
|
|
89
|
+
return MD4.safeAdd(MD4.rol(MD4.safeAdd(MD4.safeAdd(a, q), MD4.safeAdd(x, t)), s), b);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
static ff(a: number, b: number, c: number, d: number, x: number, s: number): number {
|
|
93
|
+
return MD4.cmn((b & c) | ((~b) & d), a, 0, x, s, 0);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
static gg(a: number, b: number, c: number, d: number, x: number, s: number): number {
|
|
97
|
+
return MD4.cmn((b & c) | (b & d) | (c & d), a, 0, x, s, 1518500249);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
static hh(a: number, b: number, c: number, d: number, x: number, s: number): number {
|
|
101
|
+
return MD4.cmn(b ^ c ^ d, a, 0, x, s, 1859775393);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
static safeAdd(x: number, y: number): number {
|
|
105
|
+
const lsw = (x & 0xFFFF) + (y & 0xFFFF);
|
|
106
|
+
const msw = (x >> 16) + (y >> 16) + (lsw >> 16);
|
|
107
|
+
return (msw << 16) | (lsw & 0xFFFF);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
static rol(num: number, cnt: number): number {
|
|
111
|
+
return (num << cnt) | (num >>> (32 - cnt));
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
static str2binl(str: string): number[] {
|
|
115
|
+
const bin: number[] = [];
|
|
116
|
+
const mask = (1 << MD4.chrsz) - 1;
|
|
117
|
+
for (let i = 0; i < str.length * MD4.chrsz; i += MD4.chrsz) {
|
|
118
|
+
bin[i >> 5] |= (str.charCodeAt(i / MD4.chrsz) & mask) << (i % 32);
|
|
119
|
+
}
|
|
120
|
+
return bin;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
static binl2str(bin: number[]): string {
|
|
124
|
+
let str = '';
|
|
125
|
+
const mask = (1 << MD4.chrsz) - 1;
|
|
126
|
+
for (let i = 0; i < bin.length * 32; i += MD4.chrsz) {
|
|
127
|
+
str += String.fromCharCode((bin[i >> 5] >>> (i % 32)) & mask);
|
|
128
|
+
}
|
|
129
|
+
return str;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
static get chrsz(): number {
|
|
133
|
+
return 8;
|
|
134
|
+
}
|
|
135
|
+
}
|