How to generate openssl blowfish without certain characters or allow certain set of characters

Viewed 1279

I have the following bash script to insert blowfish automatically into config.inc.php for phpMyAdmin:

#!/bin/bash

randomBlowfishSecret=$(openssl rand -base64 32)
echo "BlowFish Value: ${randomBlowfishSecret}"
replace_pma_blowfish="\$cfg['blowfish_secret'] = '${randomBlowfishSecret}'; \/* YOU MUST FILL IN THIS FOR COOKIE AUTH! *\/"
sed -i "s/\$cfg\[.blowfish_secret.\]\s*=.*/${replace_pma_blowfish}/" /var/www/html/phpMyAdmin/config.inc.php

It works but if the blowfish key contains this character '/', I will get sed error like:

root@test:./test
BlowFish Value: omaRJZpTeWZPQU+dqDc7UlrXZnL6j8i0wSE/3kTnjLU=
sed: -e expression #1, char 99: unknown option to `s'

Is there a way to generate openssl 32 character and without this character '/' or maybe how do we allow certain set of characters in openssl like this set of character:

!#%+23456789:=?@ABCDEFGHJKLMNPRS

To prevent bug in script, I want to avoid using certain characters like '/' '$' and others. I know escaping the slash is working but I don't want to use this character because it's going to have bug with another codes and I don't want to change the other codes an example of this would be roundcube that won't accept character ':' and '@' and I think this character '/' would have problem with another codes as well. So I don't want openssl to generate this character '/'

Requirements:

  1. Allow some special characters like '[' ']' '?' and avoid some characters like '/' '$'
  2. Password length is 32
2 Answers

Switch from -base64 to -hex to avoid / in generated passord.

sed will always fail if unescaped delimiter character is found in generated value. You can use this gnu awk that does it with replacement as plain text:

awk -i inplace 'BEGIN {val=ARGV[2]; --ARGC} /\$cfg\[.blowfish_secret.\][[:blank:]]*=/ {$0=val} 1' /var/www/html/phpMyAdmin/config.inc.php "$replace_pma_blowfish"

To make it readable:

awk -i inplace 'BEGIN {
   val = ARGV[2]
   --ARGC
}
/\$cfg\[.blowfish_secret.\][[:blank:]]*=/ {
   $0 = val
} 1' /var/www/html/phpMyAdmin/config.inc.php "$replace_pma_blowfish"
Related