How to encode SSH keys into base64 string without newlines - linux

What if you want to put your ssh private or public key into environment variable and access it on a CI system?
A key looks like this, so how can you convert it in a base64 string without newlines?
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACAICS0Scec9oD2raCs5HoZyQuZCPXJAVZvIJ+OooR0faAAAAJBsx4YgbMeG
IAAAAAtzc2gtZWQyNTUxOQAAACAICS0Scec9oD2raCs5HoZyQuZCPXJAVZvIJ+OooR0faA
AAAEDd1JmV4ligped6DH18jnlyEriUfNve+80vexKOOZjUwQgJLRJx5z2gPatoKzkehnJC
5kI9ckBVm8gn46ihHR9oAAAABmF3c2JvdAECAwQFBgc=
-----END OPENSSH PRIVATE KEY-----

First generate your keys or you can use any existing keys whether it's RSA or ED25519
ssh-keygen -t ed25519 -C "coo#abc.com"
Encode it into base64
cat id_ed25519 | base64 | tr -d \\n
Now you can copy paste the output anywhere you want, this should give you a string with 0 newlines.
To verify
echo your_encoded_string | base64 --decode
You should see the same key as you had in your file.

A quickest way is simply:
cat <filename> | base64 -w 0

Related

Weird hash output

I'm trying to create a hash for files in the directory using this script:
for file in *.zip; do openssl dgst -sha256 -binary ${file%.*}.zip $file | base64 >> ${file%.*}.zip.base64sha256; done
It creates hash like this:
b5iQL1fo5r+6osykGr0mcEZ14Xdbn8y0SrFGIuzMfeRvmJAvV+jmv7qh7OUavSZwRnXhd1ufzLRKsUYi7Mx95A==
But for terraform and AWS Lambdas I need a shorted hash value. I can get by using terminal and command like this:
openssl dgst -sha256 -binary archive.zip | base64 >> hash.base64sha256
And output is b5iQL1fo5r+6osykGr0mcEZ14Xdbn8y0SrFGIuzMfeQ=
So the question is: how I can retrieve short version of hash? It's required by terraform and AWS (when hash value is long - lambda are going to redeploy every time)
If you decode the "long" base64 you'll see that it's the same sequence of bytes repeated. That's because here
openssl dgst -sha256 -binary ${file%.*}.zip $file
you are specifying the file twice, once removing the extension and then re-adding it as .zip in ${file%.*}.zip, the other plainly as $file. This results in outputting the concatenated hash for both inputs (that are the same). To fix this, just specify it once:
openssl dgst -sha256 -binary "$file"
(with quotes to avoid problems with whitespace in shell expansion)
Instead of
for file in *.zip; do openssl dgst -sha256 -binary ${file%.*}.zip $file | base64 >> ${file%.*}.zip.base64sha256; done
try
for file in *.zip; do openssl dgst -sha256 -binary ${file%.*}.zip | base64 >> ${file%.*}.zip.base64sha256; done

azure Blob MD5 checksum and local MD5 checksum not matching

my file test.txt contains
checksum test file
when I upload into blob its md5 is
CONTENT-MD5 cvL65GNcvWFoqZUTI5oscw==
when I run in local md5Sum test.txt its value is
72f2fae4635cbd6168a99513239a2c73
As discussed in the comments. Solution from here:
Googled around and found a suggestion to use openssl dgst, and it
worked!
openssl dgst -md5 -binary $filename | base64
Turns out, md5sum returns a hex representation of the hash and I had
to unhex it before computing its base64:
md5sum --binary $filename | awk '{print $1}' | xxd -p -r | base64

Encoding base64 image string into JSON and decoding it back using jq

I have these 3 commands those work correctly and encode the image as json.
encoded_string=$(base64 volunteers.jpg)
payload="{\"instances\": [{\"image\": {\"b64\": \"$encoded_string\"}}]}"
echo $payload >input.json
But how do I convert it back to jpg format? This returns an error "base64: invalid input"
cat '/"' input.json '/"' | jq -r '.instances[0].image.b64' | base64 -d >output.jpg
The problem most likely is due to the embedded newlines that that are created during the encoding part of the image. You can just remove them by using tr -d \\n in your original attempt which an be slightly re-written succinctly with inputs from jq capabilities to read from standard input.
jq -Rn '.instances[0].image.b64 = inputs' < <(base64 volunteers.jpg | tr -d \\n) > input.json
The -n part is to avoid jq reading a separate input stream of its own and -R for reading raw input. Here we feed the encoded content as if it were in a file using bash process substitution syntax <(..) and feed this created file to jq
and then decoding back the created JSON as
jq -r '.instances[0].image.b64' input.json | base64 -d > output.jpg
Re-writing your original attempt with minor enhancements and without using a temporary file for storing the JSON
JSON='{"instances": [{"image": {"b64": "'"$(base64 volunteers.jpg | tr -d \\n)"'" }}]}'
jq -r '.instances[0].image.b64' <<<"$JSON" | base64 -d >output.jpg
or use printf() inplace of the here-strings(<<<)
printf '%s\n' "$JSON" | jq -r '.instances[0].image.b64' | base64 -d >output.jpg
The invocation
cat '/"' input.json '/"'
is hopelessly muddled: cat input.json would suffice. Even better, assuming input.json contains valid JSON, you could write:
< input.json jq -r '.instances[0].image.b64' | base64 -d >output.jpg

How to get rid off annoying BEGIN CERTIFICATE from certutil output?

I am trying to use certutil with its basic syntax to encode a string that shows me more than what i need. The output of the following
command:
certutil -encode pass.txt
output:
-----BEGIN CERTIFICATE-----
Z29sZGVuZ2F0ZTEyMw==
-----END CERTIFICATE-----
Please tell me how to use certutil command to get rid off these lines
-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----
OS: Windows 7
certutil -encode pass.txt | grep -v CERTIFICATE will work
edit:
grep -v removes lines that matches the regex provided, so this will remove any line containing the phrase "CERTIFICATE"

How to use two grep lines in Python

I have the two lines in bash
grep -L "BEGIN RSA PRIVATE KEY" *
grep -l "BEGIN RSA PRIVATE KEY" *
And would like to use them in a Python array
I've tried to get it working with a subprocess, but the "*" was not working.
I get a file not found error from grep.

Resources