Skip to content

Commit 37c25f9

Browse files
committed
Added encrypting/decrypting numbers.
1 parent 0da36b5 commit 37c25f9

3 files changed

Lines changed: 29 additions & 4 deletions

File tree

changes.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@
9595
- added: `SchemaEntity.clear()` for removing all current definition
9696
- added: new view engine markup `@{#}` for simulating of root URL
9797
- added: `String.ROOT()` for replacing `@{#}` markup in strings
98+
- added: `Number.encrypt(key)` for encrypting number values
99+
- added: `String.decryptnumber(key)` for decrypting of number values
100+
- added: `F.config['secret-numbers']` as a hidden secret for encrypting/decrypting values
98101

99102
- updated: (IMPORTANT) NoSQL binary divides files to independent directories for 1000 files per directory
100103
- updated: `GROUP()` by adding a new argument `url_prefix`

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,7 @@ function Framework() {
647647
version: '1.0.0',
648648
author: '',
649649
secret: this.syshash,
650+
'secret-numbers': 'numbers',
650651

651652
'security.txt': 'Contact: mailto:support@totaljs.com\nContact: https://www.totaljs.com/contact/',
652653
'etag-version': '',
@@ -8878,6 +8879,7 @@ F.$configure_configs = function(arr, rewrite) {
88788879

88798880
switch (name) {
88808881
case 'secret':
8882+
case 'secret-numbers':
88818883
obj[name] = value;
88828884
break;
88838885
case 'default-request-length':

utils.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3808,7 +3808,7 @@ function checksum(val) {
38083808
return sum;
38093809
}
38103810

3811-
String.prototype.encrypt = function(key, isUnique) {
3811+
String.prototype.encrypt = function(key, isUnique, secret) {
38123812
var str = '0' + this;
38133813
var data_count = str.length;
38143814
var key_count = key.length;
@@ -3832,10 +3832,10 @@ String.prototype.encrypt = function(key, isUnique) {
38323832
for (var i = 0; i < str.length; i++)
38333833
sum += str.charCodeAt(i);
38343834

3835-
return (sum + checksum(F.config.secret + key)) + '-' + str;
3835+
return (sum + checksum((secret || F.config.secret) + key)) + '-' + str;
38363836
};
38373837

3838-
String.prototype.decrypt = function(key) {
3838+
String.prototype.decrypt = function(key, secret) {
38393839

38403840
var index = this.indexOf('-');
38413841
if (index === -1)
@@ -3846,7 +3846,7 @@ String.prototype.decrypt = function(key) {
38463846
return null;
38473847

38483848
var hash = this.substring(index + 1);
3849-
var sum = checksum(F.config.secret + key);
3849+
var sum = checksum((secret || F.config.secret) + key);
38503850
for (var i = 0; i < hash.length; i++)
38513851
sum += hash.charCodeAt(i);
38523852

@@ -3879,6 +3879,19 @@ String.prototype.decrypt = function(key) {
38793879
return counter !== (val.length + key.length) ? null : val;
38803880
};
38813881

3882+
String.prototype.decryptnumber = function(salt) {
3883+
var val = this.decrypt(salt, F.config['secret-numbers']);
3884+
var num = 0;
3885+
if (val) {
3886+
num = +val;
3887+
if (isNaN(num))
3888+
return 0;
3889+
for (var i = 0; i < salt.length; i++)
3890+
num -= salt.charCodeAt(i);
3891+
}
3892+
return num;
3893+
};
3894+
38823895
String.prototype.base64ToFile = function(filename, callback) {
38833896
var self = this;
38843897
var index = self.indexOf(',');
@@ -4061,6 +4074,13 @@ String.prototype.removeTags = function() {
40614074
return this.replace(regexpTags, '');
40624075
};
40634076

4077+
Number.prototype.encrypt = function(salt) {
4078+
var num = this;
4079+
for (var i = 0; i < salt.length; i++)
4080+
num += salt.charCodeAt(i);
4081+
return num.toString().encrypt(salt, false, F.config['secret-numbers']);
4082+
};
4083+
40644084
Number.prototype.floor = function(decimals) {
40654085
return Math.floor(this * Math.pow(10, decimals)) / Math.pow(10, decimals);
40664086
};

0 commit comments

Comments
 (0)