Skip to content

Commit 2aeee80

Browse files
committed
perf(IDEA): implementation
1 parent 6349998 commit 2aeee80

13 files changed

+666
-2
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist

.eslintrc.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
---
3+
root: true
4+
extends: plugin:coremail/standard
5+
env:
6+
jasmine: true

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,6 @@ dist
105105

106106
# IDEA
107107
.idea
108+
109+
# locked dependencies
110+
package-lock.json

README.md

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,46 @@
1-
## IDEA
1+
# IDEA
22

33
![npm](https://badges.aleen42.com/src/npm.svg) ![javascript](https://badges.aleen42.com/src/javascript.svg)
44

5-
The IDEA cypher implementation in JavaScript
5+
It is about the IDEA cypher which is implemented in JavaScript within ~10 KiB. If you want better compatibility, you may need the polyfill version `dist/idea.all.js`, which has shimmed [`Int8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int8Array) for you.
6+
7+
## Compatibility
8+
9+
- IE6+ (polyfills required for ES7- browsers)
10+
- NodeJS
11+
12+
## Install
13+
14+
```bash
15+
npm install @cormeail/idea
16+
```
17+
18+
## Usage
19+
20+
- without padding:
21+
22+
```js
23+
function paddingToBytes(str) {
24+
typeof str !== 'string' && (str = JSON.stringify(str));
25+
26+
const blockSize = 8;
27+
const srcBytes = encoder.encode(str);
28+
const len = Math.ceil(srcBytes.length / blockSize) * blockSize; // padding with \x00
29+
const src = new Int8Array(len);
30+
src.set(srcBytes);
31+
return src;
32+
}
33+
34+
const IDEA = require('@coremail/idea');
35+
const idea = new IDEA(str2bytes('private key'), /* no padding */-1);
36+
idea.encrypt(paddingToBytes('message')); // => Int8Array[]
37+
```
38+
39+
- with xor padding (ENC3 by default):
40+
41+
```js
42+
const encoder = new TextEncoder();
43+
const IDEA = require('@coremail/idea');
44+
const idea = new IDEA(str2bytes('private key'), /* ENC3 by default */197);
45+
idea.encrypt(encoder.encode('message')); // => Int8Array[]
46+
```

build.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const webpack = require('webpack');
2+
const config = require('./webpack.config');
3+
4+
// build uncompressed
5+
webpack(config(0)).run(webpackCallback);
6+
// build minimized
7+
webpack(config(1)).run(webpackCallback);
8+
9+
function log(msg) {
10+
log.logged ? console.log('') : (log.logged = true); // add blank line
11+
console.log(msg);
12+
}
13+
14+
function webpackCallback(err, stats) {
15+
if (err) {
16+
process.exit(1);
17+
}
18+
log(stats.toString({
19+
colors : true,
20+
}));
21+
}

build/ES3HarmonyPlugin.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2018 Coremail.cn, Ltd. All Rights Reserved.
3+
*/
4+
5+
// see https://github.com/inferpse/es3-harmony-webpack-plugin
6+
7+
const name = 'ES3HarmonyPlugin';
8+
9+
module.exports = class ES3HarmonyPlugin {
10+
apply({hooks, webpack : {javascript : {JavascriptModulesPlugin}}}) {
11+
// noinspection JSUnresolvedVariable
12+
hooks.compilation.tap({name}, compilation => {
13+
// noinspection JSUnresolvedVariable, JSUnresolvedFunction
14+
JavascriptModulesPlugin.getCompilationHooks(compilation).renderMain.tap({name}, replaceSource)
15+
});
16+
}
17+
};
18+
19+
function replaceSource(source) {
20+
source = source['original'] ? source['original']() : source;
21+
if (source['getChildren']) {
22+
source['getChildren']().forEach(replaceSource);
23+
} else {
24+
// pattern: RegExp|substr, replacement: newSubstr|function
25+
replacements.forEach(([pattern, replacement]) => {
26+
if (pattern.test(source.source())) {
27+
source._value = source.source().replace(pattern, replacement);
28+
}
29+
});
30+
}
31+
}
32+
33+
const toReplace = (pattern, replacement) => [
34+
new RegExp(pattern.trim().replace(/.*noinspection.*\n/g, '').replace(/[?.[\]()]/g, '\\$&').replace(/\s+/g, '\\s*'), 'g'),
35+
// trimIndent
36+
replacement.trim().replace(/^ {8}/mg, ''),
37+
];
38+
39+
/* global __webpack_require__ */// eslint-disable-line no-unused-vars
40+
// language=JS
41+
const replacements = [
42+
// @formatter:off
43+
toReplace(`
44+
__webpack_require__.d = function (exports, definition) {
45+
for (var key in definition) {
46+
// noinspection JSUnfilteredForInLoop, JSUnresolvedFunction
47+
if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
48+
// noinspection JSUnfilteredForInLoop
49+
Object.defineProperty(exports, key, { enumerable : true, get : definition[key] });
50+
}
51+
}
52+
};
53+
`, `
54+
__webpack_require__.d = function (exports, definition) {
55+
for (var key in definition) {
56+
// noinspection JSUnfilteredForInLoop, JSUnresolvedFunction
57+
if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
58+
// noinspection JSUnfilteredForInLoop
59+
exports[key] = definition[key](); // patched by ${name}
60+
}
61+
}
62+
};
63+
`),
64+
// @formatter:on
65+
66+
// remove "use strict"
67+
[/(['"])use\s+strict(['"]);?/gm, ''],
68+
];

karma.config.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2021 Coremail.cn, Ltd. All Rights Reserved.
3+
*/
4+
5+
const webpackConfig = Object.assign({}, require('./webpack.config')(1));
6+
delete webpackConfig.entry;
7+
delete webpackConfig.output;
8+
9+
module.exports = config => {
10+
config.set({
11+
webpack : webpackConfig,
12+
files : ['test/index.js'],
13+
preprocessors : {'test/index.js' : ['webpack', 'sourcemap']},
14+
frameworks : ['jasmine', 'webpack', 'detectBrowsers'],
15+
reporters : ['mocha'],
16+
singleRun : true,
17+
plugins : [
18+
'karma-jasmine',
19+
'karma-mocha-reporter',
20+
'karma-sourcemap-loader',
21+
'karma-webpack',
22+
'karma-chrome-launcher',
23+
'karma-safari-launcher',
24+
'karma-firefox-launcher',
25+
'karma-ie-launcher',
26+
'karma-edge-launcher',
27+
'karma-detect-browsers',
28+
],
29+
30+
client : {jasmine : {random : false}},
31+
32+
detectBrowsers : {
33+
usePhantomJS : false,
34+
// ref: https://github.com/karma-runner/karma-safari-launcher/issues/12
35+
'postDetection' : availableBrowser => availableBrowser.filter(name => name !== 'Safari'),
36+
},
37+
});
38+
};

0 commit comments

Comments
 (0)