README
NgCyptoStore
This library was generated with Angular CLI version 11.2.0. to store data (string,object or array of abjects) in localstore or sessionstore with crypto-js package
Table of Contents
Installation
this package require install crypto-js package
npm i crypto-js@4.0.0 ng-cryptostore
Imports and injections
import { LocalstorageService,SessionstorageService } from 'ng-cryptostore';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
constructor(private srv:SessionstorageService){}
ngOnInit(): void {
this.srv.setItem('fruits',[{name:'fraise',icons:'π'},{name:'banane',icons:'π'}])
}
}
add to constructor :
// you can use LocalstorageService or SessionstorageService
constructor(private srv: LocalstorageService) { }
All Fuctions
1: setItem(name: string, data: any, secret?: string): Promise<void>
2: getItem(name: string, secret?: string): any
3: awiatGetItem(name: string, secret?: string): Promise<any>
4: check(name: string): boolean
5: removeItem(name: string): void
6: clearAll(): void
7: getItemLength(name: string, secret?: string): Promise<number>
8: crypt(data: any, secret?: string): Promise<any>
9: decrypt(scripts: string, secret?: string): Promise<any>
Usage
(method) setItem(name: string, data: any, secret?: string): Promise
method 'setItem' store the data for example :
// store array of objects crypted
const fruitsArray = [
{ name: "fraise", icons: "π" },
{ name: "banane", icons: "π" },
];
this.srv.setItem("fruitsArray", fruitsArray);
// store object crypted
this.srv.setItem("fruit", { name: "orange", icons: "π" });
// store strings crypted
this.srv.setItem("strings", "fruits: orange,fraise,banane and ...");
// store numbers crypted
this.srv.setItem("numbers", 1234567892121);
(method) getItem(name: string, secret?: string): any
method 'getItem' read the data for example :
// get fruits array decrypted
console.log(this.srv.getItem("fruitsArray")); // Β [{β¦}, {β¦}]
// get fruit object decrypted
console.log(this.srv.getItem("fruit")); // Β {β¦}
// get fruit strings decrypted
console.log(this.srv.getItem("strings")); // Β fruits: orange,fraise,banane and ...
// get numbers decrypted
console.log(this.srv.getItem("numbers")); // Β 1234567892121
// in case the item does not exist
console.log(this.srv.getItem("this_item_does_not_exist")); // Β ""
(method) awiatGetItem(name: string, secret?: string): Promise
method 'awiatGetItem' read the data with promise for example :
// get data decrypted
this.srv.awiatGetItem("fruitsArray").then((res) => {
console.log(res); // [{β¦}, {β¦}]
});
// Or
// get data decrypted
console.log(await this.srv.awiatGetItem("fruitsArray")); // [{β¦}, {β¦}]
(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.srv.check("fruit")); // true or false
(method) removeItem(name: string): void
method 'removeItem' remove one item by name for example :
this.srv.removeItem("fruit");
(method) clearAll(): void
method 'clearAll' remove all items for example :
this.srv.clearAll();
(method) getItemLength(name: string, secret?: string): Promise
method 'getItemLength' decrypt and get length for example :
console.log(this.srv.getItemLength("fruit")); // 21
(method) crypt(data: any, secret?: string): Promise
method 'crypt' return data crypted for example :
const data = [
{ name: "fraise", icons: "π" },
{ name: "banane", icons: "π" },
];
console.log(await this.srv.crypt(data)); // U2FsdGVkX18lKfMIr8dpIGGLy...
(method) decrypt(scripts: string, secret?: string): Promise
method 'decrypt' return data decrypted for example :
const dataCrypted = "U2FsdGVkX18lKfMIr8dpIGGLy...";
console.log(await this.srv.decrypt(dataCrypted)); // [{ name: "fraise", icons: "π" },{ name: "banane", 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 crypted with token !secret token @123456
this.srv.setItem(
"fruits",
[{ name: "orange", icons: "π" }],
"!secret token @123456"
);
// get data decrypted with token !secret token @123456
this.srv.awiatGetItem("fruits", "!secret token @123456").then((res) => {
console.log(res); // [{β¦}]
});