Skip to main content

Posts

Showing posts with the label golang

Go enjoy Python3

Given a string, get a truncated string of length up to 12. The task is ambiguous, as it doesn't say anything about whether or not 12 should include terminating null character or not. None the less, let's see how one would achieve this in various languages. Let's start with python3 import sys print(sys.argv[1][:12]) Simple enough, in essence given first argument, print it up to length 12. As an added this also deals with unicode correctly that is if passed arg is 車賈滑豈更串句龜龜契金喇車賈滑豈更串句龜龜契金喇, it will correctly print 車賈滑豈更串句龜龜契金喇. (note these are just random Unicode strings to me, no idea what they stand for). In C things are slightly more verbose, but in essence, I am going to use strncpy function: #include <stdio.h> #include <string.h> void main(int argc, char *argv[]) { char res[12]; strncpy(res,argv[1],12); printf("%s\n",res); } This treats things as byte-array instead of unicode, thus for unicode test it will end up printing just 車賈滑...

Analyzing public OpenPGP keys

OpenPGP Message Format ( RFC 4880 ) well defines key structure and wire formats (openpgp packets). Thus when I looked for public key network (SKS) server setup, I quickly found pointers to dump files in said format for bootstrapping a key server. I did not feel like experimenting with Python and instead opted for Go and found http://code.google.com/p/go.crypto/openpgp/packet  library that has comprehensive support for parsing openpgp low level structures. I've downloaded the SKS dump, verified it's MD5SUM hashes (lolz), and went ahead to process them in Go. With help from http://github.com/lib/pq  and database/sql, I've written a small program to churn through all the dump files, filter for primary RSA keys (not subkeys) and inject them into a database table. The things that I have chosen to inject are fingerprint, N, E. N & E are the modulus of the RSA key pair and the public exponent. Together they form a public part of an RSA keypair. So far, nothing fancy. Ne...

cross-compile go code, including cgo

By all means cross-compiling a new language/stack is not going to be pretty, but it didn't turn out that bad. A few weeks back, I was told that go code which uses cgo (that is utilising C api calls to shared libraries exporting C interface) cannot be cross-compiled. Well, if it's just calling out a C compiler it should totally be easy to cross compile, since so much of our platform is. So there we go, first I've picked a moderately small project which only does a couple cgo calls, and check that it compiles correctly: $ sudo apt-get build-dep ubuntu-push-client $ go get launchpad.net/ubuntu-push/... $ cd $GOPATH/src/launchpad.net/ubuntu-push/ $ go build ubuntu-push-client.go Well, when your gcc is all is easy. I didn't want to polute my system, so I quickly created a chroot with go, build-dependencies in armhf architectures and cross-compiler: # Get a chroot with build-dependencies installed, I am basing on top of a click-chroot # one should be able to use...