Skip to content

Instantly share code, notes, and snippets.

@John-Appleseed
Last active August 13, 2017 03:07
Show Gist options
  • Save John-Appleseed/456c2c4292edb16e8dfbdad6bb7f394f to your computer and use it in GitHub Desktop.
Save John-Appleseed/456c2c4292edb16e8dfbdad6bb7f394f to your computer and use it in GitHub Desktop.
Prime Saver
# Prime Sieve
alias step="echo"
# prime.saver: $threads # Search for safe primes
prime.saver(){
threads=${1:-2}
for i in `seq 1 $threads`; do
time prime.hunt $i &
done
}
# ---
# prime.hunt: $thread $bits # hunt for primes
prime.hunt(){
thread=${1:-0}
bits=${2:-4096}
pid="$!"
timestamp="`timestamp`"
prime_path="/etc/ssh/prime"
# 300mb per Core. 7 * 300 = 2100M
# tmpfs /var/tmp/prime tmpfs noatime,size=2100M 0 0
prime_work="/var/tmp/prime/candidates"
prime_log="$prime_work/prime.log"
prime_file="${timestamp}_${pid}_${thread}.prime.candidates"
#step "Setting up prime workpath"
mkdir -p "$prime_work/"
mkdir -p "$prime_path/safe/"
step "Generating prime numbers to $prime_work/$prime_file for 7 minutes"
echo ssh-keygen -G $prime_work/$prime_file -b 4096
time ssh-keygen -G "$prime_work/$prime_file" -b 4096 2>&1 | tee -a "$prime_log"
step "Sifting prime candidates for safe primes 2hrs"
echo ssh-keygen -T $prime_path/safe/$prime_file -f $prime_work/$prime_file
time ssh-keygen -T "$prime_path/safe/$prime_file" -f "$prime_work/$prime_file" 2>&1 | tee -a "$prime_log"
step "Removing prime candidates cache"
echo "rm $prime_work/$prime_file"
rm "$prime_work/$prime_file"
# step "Updating Primes"
prime.update
}
# ---
# prime.update: update /etc/ssh/moduli from /etc/ssh/safe/primes
prime.update(){
step "Updating Primes"
etc_ssh="/etc/ssh"
prime_safe="$etc_ssh/prime/safe"
timestamp="`timestamp`"
target_moduli="$etc_ssh/moduli-$timestamp"
step "Merging all $prime_safe/ primes into $target_moduli"
echo "cat $prime_safe/* > $target_moduli"
cat "$prime_safe"/* > "$target_moduli"
# Preserve original moduli file
if [[ ! -L "$etc_ssh/moduli" ]];then
# -f returns true if file exists and is a regular file or symlink
# ! -L return true if only is a regular file
step "Backing up original $etc_ssh/moduli to $etc_ssh/moduli-original"
mv "$etc_ssh/moduli" "$etc_ssh/moduli-original"
fi
step "Relinking moduli_target to $etc_ssh/moduli"
echo ln -s "$target_moduli" "$etc_ssh/moduli"
ln -sf "$target_moduli" "$etc_ssh/moduli"
echo -n "Total Primes: "
cat "$etc_ssh/moduli" | wc -l
}
# ---
# prime.clean: prime cache clean
prime.clean(){
prime_work="/var/tmp/prime/candidates"
echo "rm $prime_work/*"
if [[ -e "$prime_work" ]];then
rm "$prime_work/"*
else
echo "prime_work: $prime_work does not exist"
fi
}
# ---
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment