Skip to content

Instantly share code, notes, and snippets.

@NabilNoN
Created June 10, 2020 10:10
Show Gist options
  • Save NabilNoN/239414e19becff4ff61ed69de82d16e4 to your computer and use it in GitHub Desktop.
Save NabilNoN/239414e19becff4ff61ed69de82d16e4 to your computer and use it in GitHub Desktop.
nHash Encoder Decoder For Strings in Javascript
/*
* Copyright (c) 2020. FacceApps
*/
let nHash = {
_level: 1,
type: "#",
_key: 1,
_withHex: false,
_enc: null,
_dec: null,
setKey: function (key) {
if (key === null || key === undefined) return this;
if (this._isNumber(key)) {
this._key = parseInt(key);
this._enc = null;
this._dec = null;
} else if (String(key).length > 0) {
this._enc = this._cipher(String(key));
this._dec = this._decipher(String(key));
}
return this;
},
setType: function (type = "#") {
if (type != null && type.length > 0) this.type = type;
return this;
},
setHexMod: function (mode = false) {
this._withHex = Boolean(mode);
return this;
},
setLevel: function (level = 1) {
this._level = parseInt(String(level));
return this;
},
getKey() {
return this._key;
},
setEngine: function (engineObject = {}) {
if (engineObject.hasOwnProperty("type")) this.setType(engineObject.type);
if (engineObject.hasOwnProperty("hex")) this.setHexMod(engineObject.hex);
if (engineObject.hasOwnProperty("level")) this.setLevel(engineObject.level);
if (engineObject.hasOwnProperty("key")) this.setKey(engineObject.key);
return this;
},
getEngine: function () {
return {level: this._level, type: this.type, hex: this._withHex, key: this.getKey()};
},
encrypt(data, key = null) {
if (data === null || String(data).length === 0) return null;
this.setKey(key);
data = String(data);
let shortAble = this._isValidChar(data);
data = this._chunk(data, 1);
let nHash = [];
const byteHex = n => {
let s = "0" + Number(n).toString(16);
if (shortAble) s = s.substr(-2);
return s;
};
for (let i = 0; i < data.length; i++) {
let value = data[i].charCodeAt(0) * this.getKey();
nHash.push(String((this._withHex) ? byteHex(value) : value));
}
return (this._enc) ? this._enc(nHash.join(this.type)) : nHash.join(this.type);
},
decrypt(data, key = null) {
this.setKey(key);
if (data === null || String(data).length === 0 || (String(data).indexOf(this.type) < 0 && this._dec === null)) return null;
if (this._dec) data = this._dec(data);
data = (this._withHex) ? data.split(this.type).map((hex) => parseInt(hex, 16)) : data.split(this.type);
let nHash = [];
for (let i = 0; i < data.length; i++) {
let datum = String(data[i]);
if (this._isNumber(datum)) {
var d = (datum.indexOf(".") > 0 || datum.indexOf("e") > 0 || datum.indexOf("+") > 0) ?
parseFloat(datum) : parseInt(datum);
nHash.push(String.fromCharCode(d / this.getKey()));
}
}
return nHash.join("");
}, _isNumber(str) {
let numex = /^[0-9]+$/;
return numex.test(String(str));
}, _isValidChar(str) {
let validCharRegex = /^[a-zA-Z0-9$@$!%*?&#^-_.\s+]+$/;
return validCharRegex.test(String(str));
},
_chunk(str, n) {
let ret = [];
let i;
let len;
for (i = 0, len = str.length; i < len; i += n) {
ret.push(str.substr(i, n))
}
return ret
},
_cipher: salt => {
const textToChars = text => text.split('').map(c => c.charCodeAt(0));
const byteHex = n => ("0" + Number(n).toString(16)).substr(-2);
const applySaltToChar = code => textToChars(salt).reduce((a, b) => a ^ b, code);
return text => text.split('')
.map(textToChars)
.map(applySaltToChar)
.map(byteHex)
.join('');
},
_decipher: salt => {
const textToChars = text => text.split('').map(c => c.charCodeAt(0));
const applySaltToChar = code => textToChars(salt).reduce((a, b) => a ^ b, code);
return encoded => encoded.match(/.{1,2}/g)
.map(hex => parseInt(hex, 16))
.map(applySaltToChar)
.map(charCode => String.fromCharCode(charCode))
.join('');
}
};
window.encrypt = function (d, k) {
return nHash.encrypt(d, k);
};
window.decrypt = function (d, k) {
return nHash.decrypt(d, k);
};
if (window.hasOwnProperty("$")) {
window.$.encrypt = window.encrypt;
window.$.decrypt = window.decrypt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment