Learn more about Russian war crimes in Ukraine.

Making a public key file in Go

I saw yesterday that the kinds of file that OpenSSL uses - private and public keys, certificates, and so on - are not as opaque as they might look. Two important formats used are PEM and ASN.1. ASN.1 is a bit like protobuf, with a binary encoding called DER. PEM “wraps” this binary encoding to produce an ASCII file.

These two formats are available in the Go standard library as encoding/pem and encoding/asn1. With these, we can make files interoperable with OpenSSL. For example, let’s make a public key file:

package main
import "os"
import "encoding/pem"
import "encoding/asn1"
import "math/big"
func main() {
  var publicKey struct { Modulus *big.Int; Exponent int; }
  publicKey.Modulus = big.NewInt(0)
  publicKey.Modulus.SetString("C3E448D29FCDB2F7E52ABD17712AC76E4ABD66D54F2EF182DC4562B3FA240E3FF76658E7324E441E2C16628C703FF9DEFC76006278B35E21D890E5C2225BCD5B", 16)
  publicKey.Exponent = 0x010001
  asn1Bytes, _ := asn1.Marshal(publicKey)
  file, _ := os.Create("public_key.pem")
  pem.Encode(file, &pem.Block{ Type: "RSA PUBLIC KEY", Bytes: asn1Bytes })
  file.Close()
}

The above makes a public_key.pem which follows the PKCS#1 format for public keys, defined as:

RSAPublicKey ::= SEQUENCE {
    modulus           INTEGER,  -- n
    publicExponent    INTEGER   -- e
}

We can use this

What can computers do? What are the limits of mathematics? And just how busy can a busy beaver be? This year, I’m writing Busy Beavers, a unique interactive book on computability theory. You and I will take a practical and modern approach to answering these questions — or at least learning why some questions are unanswerable!

It’s only $19, and you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

After months of secret toil, I and Andrew Carr released Everyday Data Science, a unique interactive online course! You’ll make the perfect glass of lemonade using Thompson sampling. You’ll lose weight with differential equations. And you might just qualify for the Olympics with a bit of statistics!

It’s $29, but you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

More by Jim

Tagged . All content copyright James Fisher 2017. This post is not associated with my employer. Found an error? Edit this page.