PeculiarVentures / webcrypto-liner

webcrypto-liner is a polyfill that let's down-level User Agents (like IE/Edge) use libraries that depend on WebCrypto. (Keywords: Javascript, WebCrypto, Shim, Polyfill)
MIT License
149 stars 26 forks source link

Add padding param to AES-ECB and update install script #43

Closed microshine closed 6 years ago

microshine commented 6 years ago

@YuryStrozhevsky Could you check this version?

Also I disabled warnings for webcrypto-liner.

self.PV_WEBCRYPTO_LINER_LOG = true; // disables webcrypto-liner log

Padding param

interface AesEcbParams extends Algorithm {
    padding?: boolean;
}

Example

var alg = {
    name: "AES-ECB",
    length: 256,
    padding: true,
};
var data = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5]);
crypto.subtle.generateKey(alg, false, ["encrypt", "decrypt"])
    .then((key) => {
        return crypto.subtle.encrypt(alg, key, data)
            .then((enc) => {
                return crypto.subtle.decrypt(alg, key, enc);
            })
            .then((msg) => {
                console.log(data);
                console.log(new Uint8Array(msg));
            });
    });

image

YuryStrozhevsky commented 6 years ago

@microshine I got errors during "npm run build:es2015":

npm run build:es2015

> webcrypto-liner@0.1.27 build:es2015 D:\_Work\PROG\Ryan\webcrypto-liner-f6baf7e2ed62d4f1bb5e4c78fdbf3ce04f201bde
> tsc -p tsconfig.es2015.json

src/aes/crypto.ts(1,84): error TS2307: Cannot find module 'webcrypto-core'.
src/aes/crypto.ts(59,31): error TS2554: Expected 0 arguments, but got 2.
src/aes/crypto.ts(89,31): error TS2554: Expected 0 arguments, but got 2.
src/aes/crypto.ts(181,19): error TS2554: Expected 0 arguments, but got 3.
src/ec/crypto.ts(1,71): error TS2307: Cannot find module 'webcrypto-core'.
src/ec/crypto.ts(192,27): error TS2554: Expected 0 arguments, but got 1.
src/ec/crypto.ts(221,27): error TS2554: Expected 0 arguments, but got 1.
src/ec/crypto.ts(231,19): error TS2554: Expected 0 arguments, but got 3.
src/error.ts(1,32): error TS2307: Cannot find module 'webcrypto-core'.
src/helper.ts(89,34): error TS2488: Type must have a '[Symbol.iterator]()' method that returns an iterator.
src/init.ts(22,28): error TS2304: Cannot find name 'NativeCrypto'.
src/init.ts(23,26): error TS2304: Cannot find name 'NativeSubtleCrypto'.
src/init.ts(49,15): error TS2554: Expected 0 arguments, but got 3.
src/key.ts(1,40): error TS2304: Cannot find name 'NativeCryptoKeyPair'.
src/key.ts(6,35): error TS2304: Cannot find name 'NativeCryptoKey'.
src/rsa/crypto.ts(1,84): error TS2307: Cannot find module 'webcrypto-core'.
src/rsa/crypto.ts(50,31): error TS2554: Expected 0 arguments, but got 2.
src/rsa/crypto.ts(75,39): error TS2554: Expected 0 arguments, but got 2.
src/rsa/crypto.ts(94,39): error TS2554: Expected 0 arguments, but got 2.
src/rsa/crypto.ts(99,31): error TS2554: Expected 0 arguments, but got 2.
src/rsa/crypto.ts(123,39): error TS2554: Expected 0 arguments, but got 2.
src/rsa/crypto.ts(147,39): error TS2554: Expected 0 arguments, but got 2.
src/rsa/crypto.ts(156,31): error TS2554: Expected 0 arguments, but got 2.
src/rsa/crypto.ts(180,39): error TS2554: Expected 0 arguments, but got 2.
src/rsa/crypto.ts(188,31): error TS2554: Expected 0 arguments, but got 2.
src/rsa/crypto.ts(212,39): error TS2554: Expected 0 arguments, but got 2.
src/rsa/crypto.ts(220,31): error TS2554: Expected 0 arguments, but got 2.
src/rsa/crypto.ts(304,27): error TS2554: Expected 0 arguments, but got 1.
src/rsa/crypto.ts(304,53): error TS2339: Property 'NOT_SUPPORTED' does not exist on type 'typeof LinerError'.
src/rsa/crypto.ts(334,27): error TS2554: Expected 0 arguments, but got 1.
src/rsa/crypto.ts(334,53): error TS2339: Property 'NOT_SUPPORTED' does not exist on type 'typeof LinerError'.
src/rsa/crypto.ts(341,19): error TS2554: Expected 0 arguments, but got 3.
src/sha/crypto.ts(1,71): error TS2307: Cannot find module 'webcrypto-core'.
src/sha/crypto.ts(12,27): error TS2554: Expected 0 arguments, but got 3.
src/sha/crypto.ts(22,31): error TS2554: Expected 0 arguments, but got 1.
src/subtle.ts(2,71): error TS2307: Cannot find module 'webcrypto-core'.
src/subtle.ts(3,23): error TS2307: Cannot find module 'webcrypto-core'.
src/subtle.ts(4,47): error TS2307: Cannot find module 'webcrypto-core'.
src/subtle.ts(28,27): error TS2554: Expected 0 arguments, but got 1.
src/subtle.ts(140,31): error TS2554: Expected 0 arguments, but got 2.
src/subtle.ts(213,31): error TS2554: Expected 0 arguments, but got 2.
src/subtle.ts(260,31): error TS2554: Expected 0 arguments, but got 2.
src/subtle.ts(297,31): error TS2554: Expected 0 arguments, but got 2.
src/subtle.ts(297,57): error TS2339: Property 'NOT_SUPPORTED' does not exist on type 'typeof LinerError'.
src/subtle.ts(335,31): error TS2554: Expected 0 arguments, but got 2.
src/subtle.ts(335,57): error TS2339: Property 'NOT_SUPPORTED' does not exist on type 'typeof LinerError'.
src/subtle.ts(386,31): error TS2554: Expected 0 arguments, but got 2.
src/subtle.ts(386,57): error TS2339: Property 'NOT_SUPPORTED' does not exist on type 'typeof LinerError'.
src/subtle.ts(427,35): error TS2554: Expected 0 arguments, but got 2.
src/subtle.ts(427,61): error TS2339: Property 'NOT_SUPPORTED' does not exist on type 'typeof LinerError'.
src/subtle.ts(467,31): error TS2554: Expected 0 arguments, but got 2.
src/subtle.ts(467,57): error TS2339: Property 'NOT_SUPPORTED' does not exist on type 'typeof LinerError'.
src/subtle.ts(522,35): error TS2554: Expected 0 arguments, but got 2.
src/subtle.ts(522,61): error TS2339: Property 'NOT_SUPPORTED' does not exist on type 'typeof LinerError'.
src/subtle.ts(558,27): error TS2554: Expected 0 arguments, but got 1.
src/subtle.ts(577,31): error TS2554: Expected 0 arguments, but got 2.
src/subtle.ts(643,31): error TS2554: Expected 0 arguments, but got 2.
src/subtle.ts(757,23): error TS2554: Expected 0 arguments, but got 2.
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! webcrypto-liner@0.1.27 build:es2015: `tsc -p tsconfig.es2015.json`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the webcrypto-liner@0.1.27 build:es2015 script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
microshine commented 6 years ago

I'll fix it. I moved TS files .npmignore

YuryStrozhevsky commented 6 years ago

@microshine Same errors I can see from latest commit.

microshine commented 6 years ago

@YuryStrozhevsky I updated tsconfig.es2015.json. Could you check it again?

YuryStrozhevsky commented 6 years ago

@microshine No errors. I will test the "padding" parameter soon.

YuryStrozhevsky commented 6 years ago

@microshine Trying to run "npm run build:rollup"

npm run build:rollup

> webcrypto-liner@0.1.27 build:rollup D:\_Work\PROG\Ryan\webcrypto-liner-58e9c2383c2746f24e6a9adf3f91f8018491fe1f\webcrypto-liner-58e9c2383c2746f24e6a9adf3f91f8018491fe1f
> npm run build:es2015 && rollup -o=index.js -i=build/index.js

> webcrypto-liner@0.1.27 build:es2015 D:\_Work\PROG\Ryan\webcrypto-liner-58e9c2383c2746f24e6a9adf3f91f8018491fe1f\webcrypto-liner-58e9c2383c2746f24e6a9adf3f91f8018491fe1f
> tsc -p tsconfig.es2015.json

build/index.js → index.js...
[!] Error: You must specify options.format, which can be one of 'amd', 'cjs', 'es', 'iife' or 'umd'
https://github.com/rollup/rollup/wiki/JavaScript-API#format
Error: You must specify options.format, which can be one of 'amd', 'cjs', 'es', 'iife' or 'umd'
    at error (C:\Users\y-str\AppData\Roaming\npm\node_modules\rollup\dist\rollup.js:185:14)
    at checkOutputOptions (C:\Users\y-str\AppData\Roaming\npm\node_modules\rollup\dist\rollup.js:10175:3)
    at generate (C:\Users\y-str\AppData\Roaming\npm\node_modules\rollup\dist\rollup.js:10237:5)
    at Object.write (C:\Users\y-str\AppData\Roaming\npm\node_modules\rollup\dist\rollup.js:10280:13)
    at output (C:\Users\y-str\AppData\Roaming\npm\node_modules\rollup\bin\rollup:3281:19)
    at next (C:\Users\y-str\AppData\Roaming\npm\node_modules\rollup\bin\rollup:3235:27)
    at promise.then (C:\Users\y-str\AppData\Roaming\npm\node_modules\rollup\bin\rollup:3239:33)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
    at Function.Module.runMain (module.js:667:11)

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! webcrypto-liner@0.1.27 build:rollup: `npm run build:es2015 && rollup -o=index.js -i=build/index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the webcrypto-liner@0.1.27 build:rollup script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
microshine commented 6 years ago

I used old rollup version. After update I've got the same error What output format do you need?

'amd', 'cjs', 'es', 'iife' or 'umd'
YuryStrozhevsky commented 6 years ago

@microshine iife

microshine commented 6 years ago

@YuryStrozhevsky I set output.name to liner

YuryStrozhevsky commented 6 years ago

@microshine Better to use rollup configuration script.

D:\_Work\PROG\Ryan\webcrypto-liner-dbdc02b44719483b51a2cdbc871045651d2b3345>npm run build:rollup

> webcrypto-liner@0.1.27 build:rollup D:\_Work\PROG\Ryan\webcrypto-liner-dbdc02b44719483b51a2cdbc871045651d2b3345
> npm run build:es2015 && rollup -o=index.js -i=build/index.js --output.format=iife --output.name=liner

> webcrypto-liner@0.1.27 build:es2015 D:\_Work\PROG\Ryan\webcrypto-liner-dbdc02b44719483b51a2cdbc871045651d2b3345
> tsc -p tsconfig.es2015.json

build/index.js → index.js...
(!) Unresolved dependencies
https://github.com/rollup/rollup/wiki/Troubleshooting#treating-module-as-external-dependency
webcrypto-core (imported by build\error.js, build\subtle.js, build\aes\crypto.js, build\sha\crypto.js, build\rsa\crypto.js, build\ec\crypto.js)
(!) Missing global variable name
Use options.globals to specify browser global variable names corresponding to external modules
webcrypto-core (guessing 'core')
created index.js in 154ms

The rollup in your current configuration can not find "webcrypto-core". And thus can not create correct output package. In order to resolve the issue you need to use "rollup-plugin-node-resolve" module.

microshine commented 6 years ago

So I need to compile webcrypto-liner and put all dependencies into it. Am I right? Do you use compiled webcrypto-liner with your rollup?

microshine commented 6 years ago

webcrypto-liner has build:es2015 script which compiles TS to ES2015. package.json has "main": "build/index.js". Is it enough for using for rollup projects?

YuryStrozhevsky commented 6 years ago

@microshine In fact I was trying to build rollup package from liner many times, but always failed. Thought maybe you resolved it somehow.

But OK, left the rollup question for now. Another question: how to re-build "webcrypto-liner.shim.js" in "dist" directory with your new code?

microshine commented 6 years ago
npm run build:prod
microshine commented 6 years ago

it rebuilds on each prepare script

YuryStrozhevsky commented 6 years ago

@microshine OK, now all works fine. Hope you will merge it all to "master". Or should I approve?

microshine commented 6 years ago

This is why I moved TS to devDependencies. If you don't need to use ES2015 and you only need webcrypto-liner.shim.js or webcrypto-liner.lib.js, I suggest to move source TS files to .npmignore

YuryStrozhevsky commented 6 years ago

@microshine Seems that ES2015 files are useless at the moment. Then I suggest to remove also your package.json scripts related to the ES2015 code generation.

microshine commented 6 years ago

@YuryStrozhevsky Thank you. I'll merge and publish it soon

microshine commented 6 years ago

@YuryStrozhevsky new version of liner is available in npm