Skip to content

Commit

Permalink
feat: open project
Browse files Browse the repository at this point in the history
  • Loading branch information
willgm committed May 25, 2020
0 parents commit 105b8a1
Show file tree
Hide file tree
Showing 12 changed files with 14,722 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#root = true

[*]
indent_style = space
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 100
indent_size = 2

[*.md]
trim_trailing_whitespace = false
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
node_modules
coverage
.nyc_output
.DS_Store
*.log
.vscode
.idea
dist
compiled
.awcache
.rpt2_cache
docs
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2020 William Grasel <d.ru.fo.s+os@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Web Crypto Tools

This is has a set of tools to facilitate and give good defaults for use of the native **[Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API)**.

This project depends on the browser implementation of [Crypto API](https://caniuse.com/#feat=cryptography) and [TextEncoder API](https://caniuse.com/#feat=textencoder), which are both current implemented on all green browsers. If you do need to support IE or any older browser, you should look for available polyfills.

The native browser implementation of crypto algorithms are much faster and secure than any other JavaScript library out there. But at the same time, it is a low level API that relies on you to decide every little detail of it, so this project will give you good defaults and a better developer experience, until still let's you decide if you prefer to use other algorithms or extra protections. Be aware that, even if this project facilitates the use of the Web Crypto API, it will not prevent you from make any mistakes if you have no idea about cryptography concepts, so take your time to study a little before use it in a real project.

In the end, this is a simple collection of stateless functions, values and types, that can be individually imported and used. The minified project has currently only about 3kb in total, but it is also tree-shaking friendly, so you can end up using even less.

## Usage

### Install the project

```bash
npm install @webcrypto/tools --save
```

### Encrypt everything

```ts
import {
generateBaseCryptoKey,
deriveCryptKey,
encryptValue,
decryptValue,
} from '@webcrypto/tools';

// get any data, string or typed arrays
const originalData = 'any data';

// create a secure base key that cannot be reverted to the original key value
const baseKey = await generateBaseCryptoKey('any raw key');

// create new keys for each crypto operation from the base key
const cryptoKey = await deriveCryptKey(baseKey, generateSalt());

// encrypt any value with military level security
const [cryptoValue, nonce] = await encryptValue(originalData, cryptoKey);

// decrypt your value when necessary
const decryptedValue = await decryptValue(cryptoValue, cryptoKey, nonce);

// the decrypted value should be the same of the original
expect(originalData).toEqual(decode(decryptedValue));
```

## Documentation

The documentation with all available API and options is available.

The test cases are also quite readable and can be used as example for all the possible uses of the API.

## License

[MIT](/LICENSE)
73 changes: 73 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Karma configuration

module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',

// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine', 'karma-typescript'],

// list of files / patterns to load in the browser
files: ['test/*.test.ts', 'src/*.ts'],

// list of files / patterns to exclude
exclude: [],

// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'**/*.ts': 'karma-typescript', // *.tsx for React Jsx
},

// Config: https://www.npmjs.com/package/karma-typescript
karmaTypescriptConfig: {
exclude: ['tools'],
coverageOptions: {
threshold: {
global: {
statements: 100,
branches: 100,
functions: 100,
lines: 100,
},
},
},
reports: {
html: 'coverage',
'text-summary': '',
},
},

// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['mocha', 'karma-typescript'],

// web server port
port: 9876,

// enable / disable colors in the output (reporters and logs)
colors: true,

// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,

// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,

// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['ChromeHeadless'],

// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,

// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity,
});
};
Loading

0 comments on commit 105b8a1

Please sign in to comment.