Created
August 2, 2023 13:57
-
-
Save SamJakob/c81dc54fe690faaf26eb0b83a1c402f0 to your computer and use it in GitHub Desktop.
random number from GPG key
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
const { promisify } = require('util'); | |
const exec = promisify(require('child_process').exec); | |
/** Returns a random byte as a floating-point decimal between 0 and 1. */ | |
async function random() { | |
const payload = await exec(`gpg-connect-agent "SCD RANDOM 1" /bye`); | |
const randomBytes = payload.stdout.split('\n')[0].substr(2); | |
const buffer = Buffer.from(randomBytes, 'utf-8'); | |
const number = buffer.readUIntBE(0, 1); | |
return number; | |
} | |
(async () => { | |
const lowerLimit = 1; | |
const upperLimit = parseInt(process.argv[2], 10); | |
let rand; | |
do { | |
rand = (await random()) + lowerLimit; | |
} while (rand > upperLimit); | |
console.log(rand); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment