Last active
June 15, 2020 00:53
-
-
Save Sarverott/88c77201820c88b59cb7d51939e1fa4f to your computer and use it in GitHub Desktop.
solution to easy compression of MAC and IPv4 adresses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
ipac-maco.js | |
Sett Sarverott | |
2020 | |
*/ | |
function ipacCoding(ipAddress){ | |
//console.log(ipAddress); | |
var cryptoip=[]; | |
ipAddress.split(".").forEach(function(data){ | |
cryptoip.push( | |
parseInt(data)%16, | |
Math.floor(parseInt(data)/16) | |
); | |
}); | |
//console.log(cryptoip); | |
var output="" | |
for(var i=0; i<8; i++){ | |
output+=interfaceCharCoding( | |
cryptoip[i] | |
); | |
} | |
return output; | |
} | |
function macoCoding(macAddress){ | |
var cryptomac=[]; | |
//console.log(macAddress.split("")) | |
macAddress.split("").filter( | |
function(data){ | |
return data!=":"; | |
} | |
).forEach( | |
function(data){ | |
if(isNaN(data)){ | |
cryptomac.push( | |
(10+data.toLowerCase().charCodeAt(0)-97)%4, | |
Math.floor((10+data.toLowerCase().charCodeAt(0)-97)/4) | |
); | |
}else{ | |
cryptomac.push( | |
parseInt(data)%4, | |
Math.floor(parseInt(data)/4) | |
); | |
} | |
} | |
); | |
var output="" | |
for(var i=0; i<8; i++){ | |
output+=interfaceCharCoding( | |
cryptomac[i*3]+cryptomac[i*3+1]*4+cryptomac[i*3+2]*16 | |
); | |
} | |
return output; | |
} | |
function interfaceCharCoding(data){ | |
if(data<0){ | |
throw "to_low"; | |
} | |
if(data==0){ | |
return String.fromCharCode(46); | |
} | |
data--; | |
if(data<10){ | |
return String.fromCharCode(48+data); | |
} | |
data-=10; | |
if(data<26){ | |
return String.fromCharCode(65+data); | |
} | |
data-=26; | |
if(data<26){ | |
return String.fromCharCode(97+data); | |
} | |
data-=26; | |
if(data==0){ | |
return String.fromCharCode(95); | |
} | |
throw "to_high"; | |
} | |
//console.log(macoCoding("01:23:45:67:89:ab")); | |
//console.log(ipacCoding("127.0.0.1")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment