Created
August 17, 2017 07:54
-
-
Save tobowers/8118cd1a668926c40909639a2b2af195 to your computer and use it in GitHub Desktop.
golang errors when just marshaling and then unmarshaling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"crypto/rand" | |
"crypto/rsa" | |
"golang.org/x/crypto/ssh" | |
) | |
func main() { | |
certPrivateKey, err := rsa.GenerateKey(rand.Reader, 512) | |
if err != nil { | |
log.Fatalf("Error generating cert key: %v", err) | |
} | |
certPublicKey, err := ssh.NewPublicKey(&certPrivateKey.PublicKey) | |
if err != nil { | |
log.Fatalf("Error generating ssh public key: %v", err) | |
} | |
cert := &ssh.Certificate{ | |
Key: certPublicKey, | |
CertType: 1, | |
KeyId: "{requester: \"bob\"}", | |
ValidPrincipals: []string{"alice"}, | |
} | |
signer, err := ssh.NewSignerFromKey(certPrivateKey) | |
if err != nil { | |
log.Fatalf("Error creating signer: %v", err) | |
} | |
err = cert.SignCert(rand.Reader, signer) | |
if err != nil { | |
log.Fatalf("Error signing certificate: %v", err) | |
} | |
marshaled := cert.Marshal() | |
parsedCert := &ssh.Certificate{} | |
err = ssh.Unmarshal(marshaled, parsedCert) | |
if err != nil { | |
log.Fatalf("error unmaarshaling: %v", err) | |
} | |
if err != nil { | |
log.Fatalf("error parsing cert: %v", err) | |
} | |
if parsedCert.CertType != cert.CertType { | |
log.Fatalf("Error, parsed certType did not match %s", cert.CertType) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment