reflection - What's the proper way to extract unexported fields from a Golang type? -


i'm trying take ssh public key (x.crypto.ssh.publickey, generated parsing authorized_keys) , extract plain old crypto.rsa.publickey (i know key of type ssh-rsa). means, in ssh-land, key of (unexported) type rsapublickey.

by using either reflect.interface() or plain old fmt.sprintf("%v") can value looks like:

&{133141753607255377833524803712241410600224583899447743985716492314976605735038858392516407436607315136967439536840885454950028631410155683051419836325912444338003188332463816050354540934271243064035037856360864190161298769948076508355604915775510460445701536855931828420791030023079646791573156484306628882221 35} 

i know, reading ssh package source, ssh.rsapublickey in fact defined type want:

type rsapublickey rsa.publickey 

and therefore value thing want. (in fact, can, using "%+v", see fields (of course) named n , e.)

my problem don't know better way make go believe in fact rsa.publickey extract each of numbers strings fmt.sscan, , convert big.int , int, respectively, , use 2 values got modulus , exponent create new rsa.publickey.

that works, seems incredibly nasty. since it's unexported, there's no direct route, surely there's better way fields reconverting printable representations.

ssh.publickey have marshal() function, gives me wire-format representation, , if go through there, well, guess avoids abusing type system, i'd end writing own byte-stream-to-big-int-to-rsa converter there, doesn't seem helps. @ point might start authorized_keys file , parse myself, , seems dumb too.

what's right way this?

you can use reflect convert interface value original rsa.publickey:

rsakey := reflect.valueof(sshkey).     convert(reflect.typeof(&rsa.publickey{})).interface().(*rsa.publickey) 

playground similar example: http://play.golang.org/p/bcyhyhril_.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -