Skip to content

Instantly share code, notes, and snippets.

@TheBigSadowski
Last active April 27, 2021 11:18
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save TheBigSadowski/5105254 to your computer and use it in GitHub Desktop.
Save TheBigSadowski/5105254 to your computer and use it in GitHub Desktop.
Random IP address generator for anonymizing data.
randomByte = function() {
return Math.round(Math.random()*256);
}
randomIp = function() {
var ip = randomByte() +'.' +
randomByte() +'.' +
randomByte() +'.' +
randomByte();
if (isPrivate(ip)) return randomIp();
return ip;
}
isPrivate = function(ip) {
return /^10\.|^192\.168\.|^172\.16\.|^172\.17\.|^172\.18\.|^172\.19\.|^172\.20\.|^172\.21\.|^172\.22\.|^172\.23\.|^172\.24\.|^172\.25\.|^172\.26\.|^172\.27\.|^172\.28\.|^172\.29\.|^172\.30\.|^172\.31\./.test(ip);
}
privateIps = [
'10.0.0.0',
'10.255.255.255',
'172.16.0.0',
'172.31.255.255',
'192.168.0.0',
'192.168.255.255'
];
publicIps = [
'0.0.0.0',
'255.255.255.255',
];
for (var i = 0; i < privateIps.length; i++) {
assert(isPrivate(privateIps[i]), privateIps[i] + ' should be private, but is considered public!');
}
for (var i = 0; i < publicIps.length; i++) {
assert(!isPrivate(publicIps[i]), privateIps[i] + ' should be public, but was considered private!');
}
@imbdb
Copy link

imbdb commented Oct 9, 2016

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment