8000 Encryption · SaffronCode/SaffronCodeJS@b7ebb59 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Jul 10, 2024. It is now read-only.

Commit b7ebb59

Browse files
committed
Encryption
1 parent 42bc612 commit b7ebb59

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "saffroncodejs",
3-
"version": "1.4.6",
3+
"version": "1.5.4",
44
"description": "Package of optimised react components and javascript functions for developers ♫♪",
55
"main": "./lib/SaffronCode.js",
66
"types": "./lib/SaffronCode.d.ts",

src/SaffronCode.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import JSFunctions from './libs/JSFunctions';
88
import StringFunctions from './libs/StringFunctions';
99
import PageData from './framework/pageManager/PageData';
1010
import GlobalStorage from './libs/GlobalStorage';
11+
import Encode from './libs/Encode';
1112

1213

1314

@@ -32,7 +33,7 @@ interface frameworkModel{
3233
var framework:frameworkModel = {
3334
EventDispatcher:EventDispatcher,
3435
PageManager:PageManager,
35-
PageData:PageData
36+
PageData:PageData,
3637
}
3738

3839

@@ -41,13 +42,15 @@ var framework:frameworkModel = {
4142
interface libsModel{
4243
JSFunctions:typeof JSFunctions,
4344
StringFunctions:typeof StringFunctions,
44-
GlobalStorage:typeof GlobalStorage
45+
GlobalStorage:typeof GlobalStorage,
46+
Encode:typeof Encode,
4547
}
4648

4749
var libs:libsModel = {
4850
JSFunctions:JSFunctions,
4951
StringFunctions:StringFunctions,
5052
GlobalStorage:GlobalStorage,
53+
Encode:Encode,
5154
}
5255

5356

src/libs/Encode.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
interface EncodeModel{
3+
encrypt:typeof encrypt ;
4+
decrypt:typeof decrypt;
5+
}
6+
7+
8+
var encrypt = function(text:string,key:string,revert:Boolean=false):string
9+
{
10+
if(text===null)
11+
return '' ;
12+
var newText:string = '' ;
13+
for(var i:number = 0 ; i<text.length ; i++)
14+
{
15+
newText += String.fromCharCode(text.charCodeAt(i)+(revert?key.charCodeAt(Math.abs(key.length-i)%key.length):key.charCodeAt(i%key.length)));
16+
}
17+
return newText ;
18+
}
19+
20+
var decrypt = function(text:string,key:string,revert:Boolean=false):string
21+
{
22+
if(text==null)
23+
return '' ;
24+
var newText:string = '' ;
25+
for(var i:number = 0 ; i<text.length ; i++)
26+
{
27+
newText += String.fromCharCode(text.charCodeAt(i)-(revert?key.charCodeAt(Math.abs(key.length-i)%key.length):key.charCodeAt(i%key.length)));
28+
}
29+
return newText ;
30+
}
31+
32+
const Encode:EncodeModel = {
33+
encrypt:encrypt,
34+
decrypt:decrypt,
35+
}
36+
37+
export default Encode;

src/libs/GlobalStorage.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
11
/**@description You can save and load any object by calling this class instead of LocalStorage */
22

3+
import Encode from './Encode';
4+
35
interface GlobalStorageModel {
46
load:(id:string)=>any,
57
save:(id:string,value:any)=>string
68
}
79

10+
const deviceKey:string = ((new Date()).getTimezoneOffset()/60)+window.screen.width+navigator.product+window.screen.height+navigator.language+window.screen.colorDepth+navigator.platform+window.screen.pixelDepth;
11+
//alert("browserLanguage:");
12+
//alert("browserPlatform:");
13+
14+
//alert("sizeScreenW:");
15+
//alert("sizeScreenH:");
16+
//alert("scrColorDepth:");
17+
//alert("scrPixelDepth:");
818

919
const GlobalStorage:GlobalStorageModel = {
1020
load:load,
1121
save:save
1222
}
1323

24+
1425
/**@description It will return a save status as a string throw the result of the function */
1526
function save(id:string,value:any):string
1627
{
28+
id = window.location.host+id ;
1729
try
1830
{
19-
localStorage.setItem(id,JSON.stringify(value));
31+
localStorage.setItem(Encode.encrypt(id,deviceKey,true),Encode.encrypt(JSON.stringify(value),deviceKey));
2032
return '' ;
2133
}
2234
catch(e)
@@ -28,14 +40,15 @@ function save(id:string,value:any):string
2840
/**@description Load the saved object as an parsed object */
2941
function load(id:string)
3042
{
31-
var loadedVal:string|null = localStorage.getItem(id) ;
43+
id = window.location.host+id ;
44+
var loadedVal:string|null = localStorage.getItem(Encode.encrypt(id,deviceKey,true)) ;
3245
if(loadedVal===null)
3346
{
3447
return null ;
3548
}
3649
else
3750
{
38-
return JSON.parse(loadedVal);
51+
return JSON.parse(Encode.decrypt(loadedVal,deviceKey));
3952
}
4053
}
4154

0 commit comments

Comments
 (0)
0