'How to encode SSH keys into base64 string without newlines

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-----


Solution 1:[1]

First generate your keys or you can use any existing keys whether it's RSA or ED25519

ssh-keygen -t ed25519 -C "[email protected]"

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

your_encoded_string | base64 --decode

You should see the same key as you had in your file.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shivam