16.0.0 β€’ Published 5 months ago

ng-cryptostore v16.0.0

Weekly downloads
180
License
MIT
Repository
github
Last release
5 months ago

NgCyptoStore

This library was generated with Angular CLI version 14.2.10. to store data (string, object or array of objects) in local stores or the session store with crypto-js package

Table of Contents

Declaration

...
import { StorageModule } from 'ng-cryptostore';

@NgModule({
  declarations: [
    ...
  ],
  imports: [
    ...
    // ( localStorage | sessionStorage | cookies )
    StorageModule.withConfig({ storageType: "localStorage" })
  ],
  providers: [...],
  bootstrap: [...]
})
export class AppModule { }

Imports and injections

import { StorageService } from 'ng-cryptostore';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

  constructor(private store:StorageService){} // <---- dependency injection (DI)

  ngOnInit(): void {
    this.store.set('fruits',[{name:'fraise',icons:'πŸ“'},{name:'banana',icons:'🍌'}])
  }
}

All Functions

set(name: string, data: any, secret?: string): Promise<void>;
get(name: string, secret?: string): any;
asyncGet(name: string, secret?: string): Promise<any>;
getEncrypted(name: string): any
remove(name: string): void;
check(name: string): boolean;
getItemLength(name: string, secret?: string): Promise<number>;
clearAll(): void;
crypt(data: any, secret?: string): Promise<any>;
decrypt(scripts: string, secret?: string): Promise<any>;

Usage

(method) set(name: string, data: any, secret?: string): Promise

method 'set' store the data for example :

// store array of objects encrypted
const fruitsArray = [
  { name: "fraise", icons: "πŸ“" },
  { name: "banana", icons: "🍌" },
];
this.store.set("fruitsArray", fruitsArray);

// store object encrypted
this.store.set("fruit", { name: "orange", icons: "🍊" });

// store string encrypted
this.store.set("strings", "fruits: orange,fraise,banana and ...");

// store numbers encrypted
this.store.set("numbers", 1234567892121);

(method) get(name: string, secret?: string): any

method 'get' read the data for example :

// get fruits array decrypted
console.log(this.store.get("fruitsArray")); // Β [{…}, {…}]

// get fruit object decrypted
console.log(this.store.get("fruit")); // Β {…}

// get fruit strings decrypted
console.log(this.store.get("strings")); // Β fruits: orange,fraise,banana and ...

// get numbers decrypted
console.log(this.store.get("numbers")); // Β 1234567892121

// in case the item does not exist
console.log(this.store.get("this_item_does_not_exist")); // Β ""

(method) asyncGet(name: string, secret?: string): Promise

method 'asyncGet' read the data with promise for example :

// get data decrypted
this.store.asyncGet("fruitsArray").then((res) => {
  console.log(res); // [{…}, {…}]
});

// Or

// get data decrypted
console.log(await this.store.asyncGet("fruitsArray")); // [{…}, {…}]

(method) getEncrypted(name: string): any

method 'getEncrypted' read the data for example :

// get data encrypted
console.log(this.store.getEncrypted("fruitsArray")); // U2FsdGVkX18lKfMIr8dpIGGLy...

(method) check(name: string): boolean

method 'check' check the existence of the key and the value for example :

// check if this item exist, if exist return true
console.log(this.store.check("fruit")); // true or false

(method) remove(name: string): void

method 'remove' remove one item by name for example :

this.store.remove("fruit");

(method) clearAll(): void

method 'clearAll' remove all items for example :

this.store.clearAll();

(method) getItemLength(name: string, secret?: string): Promise

method 'getItemLength' decrypt and get length for example :

console.log(this.store.getItemLength("fruit")); // 21

(method) crypt(data: any, secret?: string): Promise

method 'crypt' return data encrypted for example :

const data = [
  { name: "fraise", icons: "πŸ“" },
  { name: "banana", icons: "🍌" },
];

console.log(await this.store.crypt(data)); // U2FsdGVkX18lKfMIr8dpIGGLy...

(method) decrypt(scripts: string, secret?: string): Promise

method 'decrypt' return data decrypted for example :

const dataEncrypted = "U2FsdGVkX18lKfMIr8dpIGGLy...";

console.log(await this.store.decrypt(dataEncrypted)); // [{ name: "fraise", icons: "πŸ“" },{ name: "banana", icons: "🍌"}]

Options

the secret is optional but if you used a custom secret in setItem you need to store it somewhere to use it later in getItem

// set data encrypted with token !secret token @123456
this.store.set(
  "fruits",
  [{ name: "orange", icons: "🍊" }],
  "!secret token @123456"
);

// get data using token "!secret token @123456"

this.store.asyncGet("fruits", "!secret token @123456").then((res) => {
  console.log(res); // [{…}]
});

console.log(this.store.get("fruits", "!secret token @123456")); //  [{…}, {…}]

console.log(this.store.getItemLength("fruit", "!secret token @123456")); // 1
15.0.0

5 months ago

15.0.1

5 months ago

16.0.0

5 months ago

14.0.0

1 year ago

14.1.0

1 year ago

13.0.0

1 year ago

13.2.0

2 years ago

12.2.13

3 years ago

12.2.10

3 years ago

12.2.11

3 years ago

12.2.12

3 years ago

1.3.8

3 years ago

1.3.7

3 years ago

1.3.6

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

0.1.1

3 years ago

0.0.1

3 years ago