Skip to content

Commit

Permalink
chore: update README and example with custom storage and sessionStora…
Browse files Browse the repository at this point in the history
…ge [skip-ci]
  • Loading branch information
softvar committed Jun 20, 2024
1 parent 02caeb8 commit d46344d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 6 deletions.
44 changes: 38 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,14 @@ const ls = new SecureLS();

`Contructor` accepts a configurable `Object` with all three keys being optional.

| Config Keys | default | accepts |
| ----------------------- | ------------ | ---------------------------------------- |
| **encodingType** | Base64 | `base64`/`aes`/`des`/`rabbit`/`rc4`/`''` |
| **isCompression** | `true` | `true`/`false` |
| **encryptionSecret** | PBKDF2 value | String |
| **encryptionNamespace** | null | String |
| Config Keys | default | accepts |
| ----------------------- | ----------------------- | ------------------------------------------------------------------------------ |
| **encodingType** | Base64 | `base64`/`aes`/`des`/`rabbit`/`rc4`/`''` |
| **isCompression** | `true` | `true`/`false` |
| **encryptionSecret** | PBKDF2 value | String |
| **encryptionNamespace** | null | String |
| **storage** | localStorage | sessionStorage/localStorage or any storage object having same methods as ls/ss |
| **metaKey** | `_secure__ls__metadata` | String |

**Note:** `encryptionSecret` will only be used for the Encryption and Decryption of data
with `AES`, `DES`, `RC4`, `RABBIT`, and the library will discard it if no encoding / Base64
Expand Down Expand Up @@ -173,6 +175,36 @@ const ls = new SecureLS({ encodingType: 'rc4', isCompression: false });
const ls = new SecureLS({ encodingType: 'rc4', isCompression: false, encryptionSecret: 's3cr3tPa$$w0rd@123' });
```

- Any other **storage** apart from localStorage

- Using `sessionStorage`

```javascript
const ls = new SecureLS({ encodingType: 'aes', isCompression: true, storage: sessionStorage });
```

- Using any storage having same methods as `localStorage/sessionStorage`

```javascript
window.customSecureLsStore = {};
const storage = {
setItem: (key, value) => {
window.customSecureLsStore[key] = value || '';
},
getItem: (key) => {
return window.customSecureLsStore[key] || null;
},
removeItem: (key) => {
delete window.customSecureLsStore[key];
},
clear: () => {
window.customSecureLsStore = {};
},
};
const ls = new SecureLS({ encodingType: 'aes', isCompression: true, storage: storage });
```

#### Methods

- **`set`**
Expand Down
29 changes: 29 additions & 0 deletions example/custom-storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
window.customSecureLsStore = {};
const storage = {
setItem: (key, value) => {
window.customSecureLsStore[key] = value || '';
},
getItem: (key) => {
return window.customSecureLsStore[key] || null;
},
removeItem: (key) => {
delete window.customSecureLsStore[key];
},
clear: () => {
window.customSecureLsStore = {};
},
};

var key = 'custom-storage';
var data = { data: [{ age: 1 }, { age: '2' }] };
var a = new SecureLS({ encodingType: '', isCompression: false, storage });
ae = a.AES.encrypt(JSON.stringify(data), 's3cr3t@123');
bde = a.AES.decrypt(ae.toString(), 's3cr3t@123');
de = bde.toString(a.enc._Utf8);

a.set(key, data);
console.log('____________________________________');
console.log('sessionStorage Case: no compression, no encryption / encoding, storage set to sessionStorage');
console.log(sessionStorage.getItem(key));
console.log(a.get(key));
console.log('____________________________________');
2 changes: 2 additions & 0 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,7 @@
<script type="text/javascript" src="rabbit-uncompressed.js"></script>
<script type="text/javascript" src="rc4-compressed.js"></script>
<script type="text/javascript" src="rc4-uncompressed.js"></script>
<script type="text/javascript" src="sessionStorage.js"></script>
<script type="text/javascript" src="custom-storage.js"></script>
</body>
</html>
13 changes: 13 additions & 0 deletions example/sessionStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var key = 'sessionStorage';
var data = { data: [{ age: 1 }, { age: '2' }] };
var a = new SecureLS({ encodingType: '', isCompression: false, storage: sessionStorage });
ae = a.AES.encrypt(JSON.stringify(data), 's3cr3t@123');
bde = a.AES.decrypt(ae.toString(), 's3cr3t@123');
de = bde.toString(a.enc._Utf8);

a.set(key, data);
console.log('____________________________________');
console.log('sessionStorage Case: no compression, no encryption / encoding, storage set to sessionStorage');
console.log(sessionStorage.getItem(key));
console.log(a.get(key));
console.log('____________________________________');

0 comments on commit d46344d

Please sign in to comment.