Skip to content

Commit 606986f

Browse files
authored
Merge pull request #11 from CodePapi/add-compare
updated to a stable version
2 parents 200b77b + ecaec28 commit 606986f

5 files changed

Lines changed: 72 additions & 13 deletions

File tree

nodemon.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"watch": ["src"],
33
"ext": ".ts,.js",
44
"ignore": [],
5-
"exec": "ts-node ./src/index.ts"
5+
"exec": "ts-node ./package/index.ts"
66
}

package.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.0.2",
44
"description": "A simplle encrypting and decrypting package",
55
"scripts": {
6-
"start": "npm run build && node build/index.js",
6+
"start": "npm run build && node dist/index.js",
77
"build": "tsc",
88
"start:dev": "nodemon",
99
"test:dev": "jest --verbose --watchAll",
@@ -24,7 +24,17 @@
2424
"^.+\\.tsx?$": "ts-jest"
2525
}
2626
},
27-
"keywords": ["encrypt", "decrypt", "n-krypta", "hash", "hashing", "encryption", "decryption"],
27+
"keywords": [
28+
"encrypt",
29+
"decrypt",
30+
"n-krypta",
31+
"hash",
32+
"hashing",
33+
"encryption",
34+
"decryption",
35+
"compare",
36+
"compare-hash"
37+
],
2838
"author": "Samuel Egbajie",
2939
"license": "ISC",
3040
"bugs": {

package/app.test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
import crayta from './app';
1+
import crpyta from './app';
22

33
const secretKey = 'my-secret-key';
44
describe('crypta', () => {
55
it('should return true', () => {
6-
expect(crayta).toBeDefined();
6+
expect(crpyta).toBeDefined();
77
});
88
it('should return encrypted string', () => {
9-
expect(crayta.encrypt('hello', secretKey)).toBe('iertw');
9+
expect(crpyta.encrypt('hello', secretKey)).toBe('#ifmmp#');
1010
});
1111
it('should return decrypted string', () => {
12-
expect(crayta.decrypt('iertw', secretKey)).toBe('hello');
12+
expect(crpyta.decrypt('#ifmmp#', secretKey)).toBe('hello');
13+
});
14+
it('should return true', () => {
15+
expect(crpyta.compare('hello', secretKey, '#ifmmp#')).toBe(true);
16+
});
17+
it('should return false', () => {
18+
expect(crpyta.compare('hello', secretKey, 'iert')).toBe(false);
1319
});
1420
});

package/app.ts

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,66 @@
11
const hash = (str: string) =>
22
str.split('').reduce((hash, char) => {
3-
hash = (hash << 5) - hash + char.charCodeAt(0);
3+
hash = 1;
44
return hash & hash;
55
}, 0);
66

77
const offset = (num: number, i: number) =>
88
parseInt(num.toString().charAt(i % num.toString().length));
99

10-
export const encrypt = (st: string, sa: string, d = 1) => {
11-
const hashSalt: number = hash(sa);
10+
const encrypta = (st: string, salt: string, d = 1) => {
11+
const hashSalt: number = hash(salt.toLocaleLowerCase().trim());
1212
return st
1313
.split('')
1414
.map((c, i) =>
1515
String.fromCharCode(c.charCodeAt(0) + offset(hashSalt, i) * d)
1616
)
1717
.join('');
1818
};
19+
20+
export const encrypt = (
21+
param: Object | string | number | any[],
22+
salt: string,
23+
d = 1
24+
) => {
25+
const hashSalt: number = hash(salt.toLocaleLowerCase().trim());
26+
return JSON.stringify(param)
27+
.split('')
28+
.map((c, i) =>
29+
String.fromCharCode(c.charCodeAt(0) + offset(hashSalt, i) * d)
30+
)
31+
.join('')
32+
.replace(/\\/g, 'blacard')
33+
.replace(/\//g, 'danger')
34+
.replace(/"/g, 'killer')
35+
.replace(/'/g, 'terror')
36+
.replace(/ /g, 'blankart')
37+
.replace(/{/g, 'alpha')
38+
.replace(/}/g, 'mega')
39+
.replace(/`/g, 'omega');
40+
};
1941
export const decrypt = (str: string, salt: string) => {
20-
return encrypt(str, salt, -1);
42+
let encryption = encrypta(
43+
str
44+
.replace(/blacard/g, '\\')
45+
.replace(/danger/g, '/')
46+
.replace(/killer/g, '"')
47+
.replace(/terror/g, "'")
48+
.replace(/blankart/g, ' ')
49+
.replace(/alpha/g, '{')
50+
.replace(/mega/g, '}')
51+
.replace(/omega/g, '`'),
52+
salt.toLocaleLowerCase().trim(),
53+
-1
54+
);
55+
try {
56+
return JSON.parse(encryption);
57+
} catch (error) {
58+
return encryption;
59+
}
60+
};
61+
62+
export const compare = (str: string, salt: string, hash: string) => {
63+
return decrypt(hash, salt) === str;
2164
};
2265

23-
export default { encrypt, decrypt };
66+
export default { encrypt, decrypt, compare };

package/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { encrypt, decrypt } from './app';
1+
export { encrypt, decrypt, compare, } from './app';

0 commit comments

Comments
 (0)