Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

webcrypto-liner

Package Overview
Dependencies
Maintainers
2
Versions
60
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

webcrypto-liner

A WebCrypto pollyfill that "smooths out" the rough-edges in existing User Agent implementations.

  • 0.1.31
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.7K
increased by46.05%
Maintainers
2
Weekly downloads
 
Created
Source

webcrypto-liner

license npm version Build Status

NPM

A polyfill for WebCrypto that "smooths out" the rough-edges in existing User Agent implementations.

Though WebCrypto is well supported across browsers, several browsers still have prefixed and buggy implementations. Additionally, they do not always support the same algorithms, for example, Edge does not support SHA1 or ECC while both Firefox and Chrome do.

NOTE: If you are not familiar with how to use the various capabilities of WebCrypto see this great example page.

webcrypto-liner is a wrapper for WebCrypto designed to address these issues, at the same time it was designed to be modular so that it can also be used for testing the addition of new algorithms to WebCrypto in the future.

Intentionally webcrypto-liner does not implement any cryptography though it does consume libraries that do. We strongly recommend you read "What’s wrong with in-browser cryptography?" before using this library.

The libraries webcrypto-liner relies on include:

PackageDescriptionSizeOptional
asmcrypto.jsA performant JavaScript implementation of popular cryptographic utilities with performance in mind.131 KBYes
ellipticFast Elliptic Curve Cryptography in plain javascript130 KBYes
webcrypto-coreA input validation layer for WebCrypto polyfills 125 KBNo

1 This library is compiled into webcrypto-liner.

webcrypto-liner will always try to use a native implementation of webcrypto, or a prefixed version of webcrypto, before it falls back to a Javascript implementation of a given algorithm. We have no control over the corresponding implementation and what it does, for example, it may not use window.crypto.getRandomValues even if it is available and the mechanism it uses to gather randomness may be both insecure and weak.

We have done no security review or take a position on the security of these third-party libraries. YOU HAVE BEEN WARNED.

To keep webcrypto-liner as small as possible (right now it is ~11kb without dependencies) it was designed to be modular, so if you do not need ECC support, do not include elliptic as a dependency and it will not be loaded.

If you do not load any of the dependencies that provide cryptographic implementations webcrypto-liner will work as an interoperability layer, very similar to webcrypto-shim.

webcrypto-liner supports the following algorithms and key lengths:

CapabilityDetails
Encryption/DecryptionRSA-OAEP, AES-CBC, and AES-GCM
Sign/VerifyRSA-PSS, RSASSA_PKCS1-v1_5 and ECDSA
HashSHA-1, and SHA-224
Derive Key/BitsECDH
KeywrapAES-GCM, AES-CBC, AES-ECB 2
ECC CurvesP-256, P-384, and P-512
RSA Key Lengths1024, 2048, 3072, and 4096
AES Key Lengths128, 192 and 256

2 ECB support is not defined by the WebCrypto specifications. Use of EBC in a safe way is hard, it was added for the purpose of enabling interoperability with an existing system. We recommend against its use unless needed for interoperability.

You can see the webcrypto-liner in use in the pv-webcrypto-tests page.

Using

<head>
    <!-- ... -->
    <!-- ... -->
    <!-- promise.js is needed for IE Promise implementation -->
    <script src="https://www.promisejs.org/polyfills/promise-7.0.4.min.js"></script>
    <script src="src/webcrypto-liner.js"></script>
</head>
<body>
    <script> 
        crypto.subtle.generateKey({name: "AES-GCM", length: 192}, true, ["encrypt", "decrypt"])
            .then(function(key){
                return crypto.subtle.encrypt({
                        name: "AES-GCM", 
                        iv: new Uint8Array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]),
                        tagLength: 128
                    }, key, new Uint8Array([1,2,3,4,5]))
            })
            .then(function(enc){
                console.log(new Uint8Array(enc));
            })
            .catch(function(err){
                console.log(err.message); // Chrome throws: 192-bit AES keys are not supported
            })
    </script>
</body>

Dependencies

typescript

npm install typescript --global

Installation

The module has been designed to be useful in ES6 and ES5 projects. The default is ES5 with commonjs, to install and build you would run:

npm install
npm run build

The package also supports rollup and webpack, if you want a ES5 build with Webpack you would use the following commands:

npm install
npm run build:webpack

If you want a ES2015 build for rollup you would use the following commands:

npm install
npm run build:es2015

FAQ

  • Do I need to use a promise library? - No, not if your browser supports promises.
  • Do I need to include asmcrypto.js? No, not unless you want to use the algorithms it exposes.
  • Do I need to include elliptic.js? No, not unless you want to use the algorithms it exposes.
  • How are random numbers generated? We use two libraries for crypto operations in Javascript, asymcrypto and ellipticjs both rely on window.crypto.getRandomValues where available. asymcrypto also has a fallback mechanism where it generates its own random numbers if not present.
  • How big is the total package? Right now, if you include all optional dependencies (minfied) the package is ~300 KB, if you include only ECC or only RSA support that is lowered to about 180 KB. Additionally you will see GZIP compression provide about 30% savings above and beyond that. If you use webcrypto-liner as just an interopability shim and do not use any of the optional third-party libraries it will be under 44 KB in size.
  • Will it work in Node? No. It is compiles to pure Javascript but uses the window object so it wont work in Node at this time. With some minor changes it should also be able to work in Node also but you really should be using node-webcrypto-ossl on Node instead.

FAQs

Package last updated on 10 Jan 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc