The FreshFx urlsafe-crypto lib.
- babel (with preset-env, plugin-add-jsdoc-properties, plugin-transform-object-reset-spread)
- eslint (with eslint-plugin-import)
- mocha, sinon & should
- conventional-changelog (use the angular cz style!)
- npm-run-all, onchange, release-it
- clone this repository
- delete the
.git
folder - change those parts:
index.js
-> adopt the module namepackage.json
-> edit the corresponding values
- remove the .npmrc file if you don't want to use FreshFx private modules
- change the settings in
.release-it.json
if you want to publish to npm - change the
template.hbs
file to fit your needs
Attention: to take advantage of the private npm modules, set the NPM_TOKEN as env variable
see package.json's "scripts" for all available commands
npm run commit
npm start
npm run release
encrypts, encodes and deflates a given object/string
Encryption Process:
- stringify data (optional)
- deflate
- encrypt
- deflate
- urlsafe base64 encode
Decryption Process:
- urlsafe base64 decode
- inflate
- decrypt
- inflate
- parse data (optional)
npm install --save @freshfx/urlsafe-crypto
Attention: in order to install this package, either be a permitted user for this package, or create
a .npmrc
in your projects root with following content (and set the NPM_TOKEN environment variable):
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
and not f.e. (iron)[https://www.npmjs.com/package/iron]?
- we don't need integrity (yet)
- the generated string is shorter (30-50%) - good for URLs since the GET url length can be limited
- @freshfx/urlsafe-crypto
- module.exports(encKey, isString) ⇒
urlsafe-crypto
⏏- static
- .encrypt ⇒
Promise
- .encryptSync ⇒
String
- .decrypt ⇒
String
|Object
- .decryptSync ⇒
String
|Object
- .encrypt ⇒
- inner
- ~urlsafe-crypto :
Object
- ~urlsafe-crypto :
- static
- module.exports(encKey, isString) ⇒
exports a init function which returns the specific methods without the need of the encryption key
Kind: Exported function
Returns: urlsafe-crypto
- urlsafe-crypto functions
Param | Type | Description |
---|---|---|
encKey | String |
encryption key |
isString | Boolean |
set the default value for isString (f.e. if you only encrypt/decrypt strings) |
Example
import urlsafeCrypto from 'rbcp3-urlsafe-crypto'
const ENC_KEY = 'b6bad4846614652e7ead69df7337a7f4'
const crypto = urlsafeCrypto(ENC_KEY)
// prints the object
crypto.encrypt({an: 'object'}).then(crypto.decrypt).then(console.log)
// prints 'test', important: add the isString option to the decrypt function
crypto.encrypt('test').then(encryptedString => crypto.decrypt(encryptedString, true)).then(console.log)
const cryptoString = urlsafeCrypto(ENC_KEY, true)
// prints 'test'
cryptoString.encrypt('test').then(crypto.decrypt).then(console.log)
// prints the object, important: add the isString option to decrypt function
cryptoString.encrypt({an: 'object'}).then(encryptedObject => cryptoString.decrypt(encryptedObject, false)).then(console.log)
encrypts and encodes a given object or string
Kind: static constant of module.exports
Returns: Promise
- Promise which resolves with the resulting String
Param | Type | Description |
---|---|---|
data | Object | String |
the data to encrypt |
encKey | String |
Encryption Key, length must be 32 (256 Bit) |
Example
const ENC_KEY = '6b7beea8ef24f7ee89e153387db8f04f'
// should print something like 'eJwNzAcBA0EIADBLbI66YfqX8I2ACCUcg2dSRseFKzIYpAtXM_yWN9_sDVwk9lxORTkS6op3FHdJdOmqnTI1rbrJP-FnD-wDMqcaoQ'
encrypt({key: 'value'}, ENC_KEY).then(result => console.log(result))
encrypts and encodes a given object or string - synchronously
Kind: static constant of module.exports
Returns: String
- the resulting string
Param | Type | Description |
---|---|---|
data | Object | String |
the data to encrypt |
encKey | String |
Encryption Key, length must be 32 (256 Bit) |
Example
const ENC_KEY = '6b7beea8ef24f7ee89e153387db8f04f'
// should print something like 'eJwNzAcBA0EIADBLbI66YfqX8I2ACCUcg2dSRseFKzIYpAtXM_yWN9_sDVwk9lxORTkS6op3FHdJdOmqnTI1rbrJP-FnD-wDMqcaoQ'
console.log(encrypt({key: 'value'}, ENC_KEY))
decodes and decrypts a given string
Kind: static constant of module.exports
Returns: String
| Object
- the resulting string/object
Param | Type | Default | Description |
---|---|---|---|
string | String |
the data to decode/decrypt | |
encKey | String |
Encryption Key, length must be 32 (256 Bit) | |
toString | Boolean |
false |
indicates if the result should be converted into a string or object |
Example
const ENC_KEY = '6b7beea8ef24f7ee89e153387db8f04f'
// prints the object'{"key": "value"}'
decrypt('eJwNzAcBA0EIADBLbI66YfqX8I2ACCUcg2dSRseFKzIYpAtXM_yWN9_sDVwk9lxORTkS6op3FHdJdOmqnTI1rbrJP-FnD-wDMqcaoQ', ENC_KEY)
.then(console.log)
// prints 'tests'
encrypt('test', ENC_KEY).then(result => decrypt(result, ENC_KEY, true)).then(console.log)
decodes and decrypts a given string - synchronously
Kind: static constant of module.exports
Returns: String
| Object
- the resulting string/object
Param | Type | Default | Description |
---|---|---|---|
string | String |
the data to decode/decrypt | |
encKey | String |
Encryption Key, length must be 32 (256 Bit) | |
toString | Boolean |
false |
indicates if the result should be converted into a string or object |
Example
const ENC_KEY = '6b7beea8ef24f7ee89e153387db8f04f'
// prints the object'{"key": "value"}'
console.log(decrypt('eJwNzAcBA0EIADBLbI66YfqX8I2ACCUcg2dSRseFKzIYpAtXM_yWN9_sDVwk9lxORTkS6op3FHdJdOmqnTI1rbrJP-FnD-wDMqcaoQ', ENC_KEY))
// prints 'tests'
console.log(decrypt(encrypt('test', ENC_KEY), ENC_KEY, true))
Kind: inner typedef of module.exports
Properties
Name | Type |
---|---|
encrypt | function |
encryptSync | function |
decrypt | function |
decryptSync | function |
© 2021 FreshFx npm@freshfx.at.