Last active
June 25, 2018 17:36
-
-
Save ansemjo/40667dd9ee072107ae75000d047a89a0 to your computer and use it in GitHub Desktop.
generate a unique local address prefix in javascript
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
#!/usr/bin/env node | |
/* generate a unique local address prefix */ | |
/* usage: `node prefix.js 56`, where 56 can be replaced by any prefix length between 8 and 128 */ | |
/* alternatively, a simple one-liner for a /48 ula prefix: | |
* node -pe "('fd' + crypto.randomBytes(5).toString('hex')).match(/.{1,4}/g).join(':') + '::/48'" | |
*/ | |
/* iife-wrapped prefix generator */ | |
console.log((function prefix() { | |
/* get n random bytes */ | |
let b = n => require('crypto').randomBytes(n); | |
/* get desired prefix length from commandline */ | |
let p = process.argv.find(e => e.match(/^[\d]+$/)) || 48; | |
/* last byte index required */ | |
let l = Math.floor(p/8); | |
/* necessary bitshift */ | |
let s = 8 - p % 8; | |
/* generate random ula prefix */ | |
let a = b(16) | |
/* first byte = fd */ | |
.fill(253, 0, 1) | |
/* zero bytes */ | |
.fill(0, l) | |
/* shift last byte */ | |
.fill(b(1).readUInt8() << s,l,Math.min(16, l+1)); | |
/* format as ipv6 string */ | |
let f = (b, p) => b.toString('hex').match(/.{1,4}/g) | |
.join(':').replace(/(:0000)+$/,'::') + `/${p}`; | |
return f(a, p); | |
})()); | |
/* node -e "$(curl -s https://gist.githubusercontent.com/ansemjo/40667dd9ee072107ae75000d047a89a0/raw/)" 56 */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment