Skip to content

Commit c988ace

Browse files
committed
updated to a stable version
1 parent 200b77b commit c988ace

5 files changed

Lines changed: 102 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: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,31 @@
1-
export { encrypt, decrypt } from './app';
1+
export { encrypt, decrypt, compare, } from './app';
2+
import { encrypt, decrypt, compare, } from './app';
3+
4+
const aSampleObject = {
5+
name: 'John',
6+
age: 30,
7+
language: 'New York',
8+
country: 'USA',
9+
address: {
10+
street: 'Main Street',
11+
number: 123,
12+
},
13+
};
14+
const anEnglishStory =
15+
"Mohandas Karamchand Gandhi (/ˈɡɑːndi, ˈɡændi/;[3] GAHN-dee; 2 October 1869 – 30 January 1948) was an Indian lawyer,[4] anti-colonial nationalist[5] and political ethicist[6] who employed nonviolent resistance to lead the successful campaign for India's independence from British rule,[7] and to later inspire movements for civil rights and freedom across the world. The honorific Mahātmā (Sanskrit: 'great-souled', 'venerable'), first applied to him in 1914 in South Africa, is now used throughout the world.[8][9] Born and raised in a Hindu family in coastal Gujarat, Gandhi trained in the law at the Inner Temple, London, and was called to the bar at age 22 in June 1891. After two uncertain years in India, where he was unable to start a successful law practice, he moved to South Africa in 1893 to represent an Indian merchant in a lawsuit. He went on to live in South Africa for 21 years. It was here that Gandhi raised a family and first employed nonviolent resistance in a campaign for civil rights. In 1915, aged 45, he returned to India and soon set about organising peasants, farmers, and urban labourers to protest against excessive land-tax and discrimination. Assuming leadership of the Indian National Congress in 1921, Gandhi led nationwide campaigns for easing poverty, expanding women's rights, building religious and ethnic amity, ending untouchability, and, above all, achieving swaraj or self-rule. Gandhi adopted the short dhoti woven with hand-spun yarn as a mark of identification with India's rural poor. He began to live in a self-sufficient residential community, to eat simple food, and undertake long fasts as a means of both introspection and political protest. Bringing anti-colonial nationalism to the common Indians, Gandhi led them in challenging the British-imposed salt tax with the 400 km (250 mi) Dandi Salt March in 1930 and in calling for the British to quit India in 1942. He was imprisoned many times and for many years in both South Africa and India.";
16+
const sampleArray = [
17+
'John',
18+
'Peter',
19+
'Sally',
20+
'Jane',
21+
'Mark',
22+
'Bob',
23+
'Alice',
24+
'Marry',
25+
'Tom',
26+
'Jerry',
27+
];
28+
let encrypted = encrypt(sampleArray, 'my-secret')
29+
console.log(encrypted);
30+
const enS="blacard#Kpio#-#Qfufs#-#Tbmmz#-#Kbof#-#Nbsl#-#Cpc#-#Bmjdf#-#Nbssz#-#Upn#-#Kfssz#^"
31+
console.log("ans", decrypt(enS, 'my-secret'))

0 commit comments

Comments
 (0)