I have to convert RSA private key to one-line to store in the password manager (Passwordstate). I used tr -d '\n' < id_rsa to convert to single line and cat id_rsa.line | sed -e "s/-----BEGIN RSA PRIVATE KEY-----/&\n/" -e "s/\S\{64\}/&\n/g" to convert back to original multi-line.
Conversion back to multi-line worked on Ubuntu, but not on Mac. Why this doesn't work on Macbook
Please try:
LF=$'\\\x0A'
cat id_rsa.line | sed -e "s/-----BEGIN RSA PRIVATE KEY-----/&${LF}/" -e "s/-----END RSA PRIVATE KEY-----/${LF}&${LF}/" | sed -e "s/[^[:blank:]]\{64\}/&${LF}/g"
or
LF=$'\\\x0A'
cat id_rsa.line | sed -e "s/-----BEGIN RSA PRIVATE KEY-----/&${LF}/" -e "s/-----END RSA PRIVATE KEY-----/${LF}&${LF}/" | fold -w 64
Non-GNU sed does not interpret "\n" in the replacement as a newline.
As a workaround, you can assign a variable to a newline and embed it in the replacement.
Note that I've kept the UUC for readability :P.
Building on top of tshiono's answer. Wrote a shell script to achieve the same:
#!/bin/sh
# Convert single-line certificate to multi-line certificate
echo "$1" > secret
LF=$'\\\x0A'; cat secret | sed -e "s/-----BEGIN RSA PRIVATE KEY-----/&${LF}/" -e "s/-----END RSA PRIVATE KEY-----/${LF}&${LF}/" | sed -e "s/[^[:blank:]]\{64\}/&${LF}/g" > secret-temp
x=`echo "-----BEGIN RSA PRIVATE KEY-----"; cat secret-temp; echo "-----END RSA PRIVATE KEY-----"`
echo "$x" > secret.pem
echo "\n\n## Private Key ##\n\n"
cat secret.pem
Related
The following command works for SHA1: csum -h SHA1 (FileName).txt > (FileName_chksum).txt. How to create a similar file using the SHA256 algorithm in AIX?
You can use the openssl command from the openssl.base package; it has a dgst sub-command that will generate a SHA256 hash of the file:
openssl dgst -sha256 filename.txt > filename_sha256.txt
By default, it will print in the following format:
SHA256(filename.txt)= hash-string-here
The csum command prints in a slightly different format:
hash-string-here filename.txt
... so you may want to rearrange the output of openssl based on your specific needs for the filename_sha256.txt file.
If you only want the hashed string itself in the new file, you could use awk:
openssl dgst -sha256 filename.txt | awk '{print $2}' > filename_sha256.txt
When I try to SHA512 some file using openssl I got the output file contents starts with something like "SHA512(in.txt)= 090c..."
I tried the different options -r, -binary with the command
Here is the command I'm using
openssl dgst -sha512 -out out.txt in.txt
The question is: How can I got the file contains only the hash, without that starting note?
You can only print the second column using awk, if the file name doesn't contain spaces:
openssl dgst -sha512 -out in.txt | awk '{print $2}' > out.txt
Or (looks like not cross-platform) you can try either pipe or reading from stdin:
openssl dgst -sha512 -out out.txt < in.txt
cat in.txt | openssl dgst -sha512 -out out.txt
This works for me (Mac OS X).
The default delimiter of awk is a space character, and the accepted answer will not work if there are spaces in the filename. You can override the default delimiter with the -F flag (field separator) to = , but that would also not work if there happens to be an equal space in the filename. Printing the last column using the default delimiter should work for all of those edge cases. The $NF awk variable stores the number of fields and can be used directly to print the last column, which should always be the hash.
openssl dgst -sha512 -out in.txt | awk '{print $NF}' > out.txt
https://linux.die.net/man/1/awk
I know we can get the right output in below ways:
echo -n 123456 | openssl md5
e10adc3949ba59abbe56e057f20f883e
or
printf 123456 | openssl md5
e10adc3949ba59abbe56e057f20f883e
or
printf 123456 > file.txt
openssl md5 file.txt
e10adc3949ba59abbe56e057f20f883e
However, I want to know could we work it out in below command-line with extra options
openssl md5 <<< '123456'
f447b20a7fcbf53a5d5be013ea0b15af( this is incorrect)
bash (and ksh93, and zsh) will always append a newline to the content of the here-string. There is no way around this apart from filtering it out explicitly.
$ tr -d '\n' <<<'123456' | openssl md5
(stdin)= e10adc3949ba59abbe56e057f20f883e
I want to replace one string with another but I can't. The code is:
updatedb
MCRYPTINI=$(locate mcrypt.ini | grep 'apache2')
MCRYPTSO=$(locate mcrypt.so | grep "/mcrypt.so")
OLD="extension=mcrypt.so"
NEW="extension=$MCRYPTSO"
echo $MCRYPTINI
echo $MCRYPTSO
echo $OLD
echo $NEW
echo "'s/$OLD/$NEW' $MCRYPTINI"
sed -i 's/$OLD/$NEW' $MCRYPTINI
And the result is:
sudo sh testScript.sh
/etc/php5/apache2/conf.d/20-mcrypt.ini
/usr/lib/php5/20121212/mcrypt.so
extension=mcrypt.so
extension=/usr/lib/php5/20121212/mcrypt.so
's/extension=mcrypt.so/extension=/usr/lib/php5/20121212/mcrypt.so' /etc/php5/apache2/conf.d/20-mcrypt.ini
sed: -e expression #1, char 11: unterminated `s' command
For the response I don't need to use 'sed', but it's looks easy and good.
I use sh not bash because I want the code can use in all the systems, so I prefer answers that follow that principle
UPDATE
sed -i "s/$OLD/$NEW/" $MCRYPTINI
error:
sed: -e expression #1, char 14: unknown option to `s'
Add a slash and double quotes:
sed -i "s/$OLD/$NEW/" file
The solution could be:
sed -i "s/$OLD/$NEW/" $MCRYPTINI
but $NEW is a path, so I need to change "/" by other character, for example "+"
sed -i "s+$OLD+$NEW+" $MCRYPTINI
I'm sure this has been asked before and I'm sorry if it has but I can't find it.
I'm trying to replace 'mypassword' in the following with a dovecot SHA512-CRYPT password but the dollar sign is screwing things up and I can't figure out how to escape it.
The password generated from doveadm pw -s SHA512-CRYPT -p ${rootemailpw} looks something like the following which you can see has a few dollar signs in it:
{SHA512-CRYPT}$6$sujue1sr0abLZisA$Dq1qtz422woyR/A4wDaJ9JYByKHis27FO.mO5EfUCDuI/PRSawxgCRGA3Oq/LE4Nbr32nCr4731BCPIotmRNR0
Here is the script I'm using:
rootemailpw=$(</dev/urandom tr -dc A-Za-z0-9 | head -c12)
rootpw=$(doveadm pw -s SHA512-CRYPT -p ${rootemailpw})
cat <<'EOF' > tmp
USE `postfix`;
INSERT INTO `users` (`username`,`password`) VALUES ('root#localhost','mypassword');
EOF
sed -i "s/mypassword/${rootpw}/" tmp
So how to escape the $ signs properly in s/mypassword/${rootpw}/
The SHA hashes may contain slash characters (/), which is the usual sed delimiter. sed supports most delimiters so one way around it is to use a different one, e.g. a pipe character (|):
pw=$(doveadm pw -s SHA512-CRYPT -p somepassword)
echo password | sed "s|password|$pw|"
Output:
{SHA512-CRYPT}$6$qtXJPWaMbX3l8zMW$mXWuax8NG9Wa1AkFdcClA5vw2T3407uBdImKNNbcBd/CUkS5NafjZnQ5ow.IubyLujU5ohS8PWogdNoPiF4d//
With the following in ./tmp:
USE `postfix`;
INSERT INTO `users` (`username`,`password`) VALUES ('root#localhost','mypassword');
Executing rootpw='1234$asdf' ; cat tmp | sed "s/mypassword/${rootpw}/" will replace mypassword with the contents of ${rootpw} like so:
USE `postfix`;
INSERT INTO `users` (`username`,`password`) VALUES ('root#localhost','1234$asdf');
for:
rootpw='1234$asdf' sets the environment variable
cat tmp | sed "s/mypassword/${rootpw}/" replaces mypassword with "1234$asdf"
Use single quotes on the variable.
Does that answer your question?