Skip to main content

Calling OpenSSL libcrypto functions from Go

I've released on github three Go packages that present the same set of functions as crypto/md5, crypto/sha1 and crypto/rc4 but that use the OpenSSL libcrypto functions instead of the native Go implementation.

Testing by sending 4.4GB of data through these three functions yields the following results (the test document was the King James Bible concatenated 1,000 times and sent to MD5, SHA1 and RC4 in 4.4MB chunks). All tests were done using go version devel +7dc8d66efb6d Mon Jan 21 10:53:39 2013 +1100 linux/amd64.

Native Go404 MB/s123 MB/s111 MB/s
Via OpenSSL607 MB/s636 MB/s744 MB/s

These packages provide the same interface as the native Go implementations and should be drop in replacements. libcrypto must be available. The code is available in my go-openssl repository.

PS As this is the first time I've written any cgo code, I'd be happy to hear from more experienced Go programmers about improvements to my wrappers.

PPS As a comparison here is the output of the Go benchmark tests on the native Go implementations of MD5 and SHA1
% go test -bench=".*" crypto/md5
BenchmarkHash8Bytes          5000000       477 ns/op  16.74 MB/s
BenchmarkHash1K              1000000      2882 ns/op 355.26 MB/s
BenchmarkHash8K               100000     20061 ns/op 408.34 MB/s
BenchmarkHash8BytesUnaligned 5000000       477 ns/op  16.76 MB/s
BenchmarkHash1KUnaligned     1000000      2875 ns/op 356.15 MB/s
BenchmarkHash8KUnaligned      100000     20147 ns/op 406.60 MB/s
ok  crypto/md5 15.997s
% go test -bench=".*" crypto/sha1
BenchmarkHash8Bytes 2000000       799 ns/op  10.00 MB/s
BenchmarkHash1K      200000      9098 ns/op 112.54 MB/s
BenchmarkHash8K       50000     67341 ns/op 121.65 MB/s
ok  crypto/sha 18.364s
And here's the same with the OpenSSL wrapped versions:
% go test -bench=".*" md5
BenchmarkHash8Bytes          5000000       611 ns/op  13.09 MB/s
BenchmarkHash1K              1000000      2272 ns/op 450.54 MB/s
BenchmarkHash8K               100000     13816 ns/op 592.92 MB/s
BenchmarkHash8BytesUnaligned 5000000       616 ns/op  12.97 MB/s
BenchmarkHash1KUnaligned     1000000      2282 ns/op 448.59 MB/s
BenchmarkHash8KUnaligned      100000     14089 ns/op 581.44 MB/s
ok  md5 15.049s
% go test -bench=".*" sha1
BenchmarkHash8Bytes 5000000       625 ns/op  12.78 MB/s
BenchmarkHash1K     1000000      2249 ns/op 455.31 MB/s
BenchmarkHash8K      100000     13507 ns/op 606.46 MB/s
ok  sha1 7.525s


Popular posts from this blog

Your last name contains invalid characters

My last name is "Graham-Cumming". But here's a typical form response when I enter it: Does the web site have any idea how rude it is to claim that my last name contains invalid characters? Clearly not. What they actually meant is: our web site will not accept that hyphen in your last name. But do they say that? No, of course not. They decide to shove in my face the claim that there's something wrong with my name. There's nothing wrong with my name, just as there's nothing wrong with someone whose first name is Jean-Marie, or someone whose last name is O'Reilly. What is wrong is that way this is being handled. If the system can't cope with non-letters and spaces it needs to say that. How about the following error message: Our system is unable to process last names that contain non-letters, please replace them with spaces. Don't blame me for having a last name that your system doesn't like, whose fault is that? Saying "Your

How to write a successful blog post

First, a quick clarification of 'successful'. In this instance, I mean a blog post that receives a large number of page views. For my, little blog the most successful post ever got almost 57,000 page views. Not a lot by some other standards, but I was pretty happy about it. Looking at the top 10 blog posts (by page views) on my site, I've tried to distill some wisdom about what made them successful. Your blog posting mileage may vary. 1. Avoid using the passive voice The Microsoft Word grammar checker has probably been telling you this for years, but the passive voice excludes the people involved in your blog post. And that includes you, the author, and the reader. By using personal pronouns like I, you and we, you will include the reader in your blog post. When I first started this blog I avoid using "I" because I thought I was being narcissistic. But we all like to read about other people, people help anchor a story in reality. Without people your bl

The Elevator Button Problem

User interface design is hard. It's hard because people perceive apparently simple things very differently. For example, take a look at this interface to an elevator: From flickr Now imagine the following situation. You are on the third floor of this building and you wish to go to the tenth. The elevator is on the fifth floor and there's an indicator that tells you where it is. Which button do you press? Most people probably say: "press up" since they want to go up. Not long ago I watched someone do the opposite and questioned them about their behavior. They said: "well the elevator is on the fifth floor and I am on the third, so I want it to come down to me". Much can be learnt about the design of user interfaces by considering this, apparently, simple interface. If you think about the elevator button problem you'll find that something so simple has hidden depths. How do people learn about elevator calling? What's the right amount of