about summary refs log tree commit homepage
path: root/t/bin
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-11-07 08:08:18 +0000
committerEric Wong <normalperson@yhbt.net>2009-11-07 00:09:41 -0800
commitb36235131e0b5517fc5070c02c72be01c4b7f1f9 (patch)
tree3c4774be5d51c905dcf9384c4ba5bc7816e7905d /t/bin
parent1a9a718a3f9a5b582a4a339a9bb9249c2ca392d7 (diff)
downloadrainbows-b36235131e0b5517fc5070c02c72be01c4b7f1f9.tar.gz
sha1sum(1) is only common GNU systems, and it may be installed
as gsha1sum on *BSDs.  We'll also try using the openssl sha1
implementation, too.  And finally, we'll provide our own Ruby
sha1sum.rb implementation as a last resort.

We go to great lengths to avoid our own Ruby version because we
want to avoid putting too much trust in ourselves, our Ruby
skills, and even the Ruby implementations.  This is especially
with regard to our knowledge and correct usage of Ruby 1.9
encoding support.  It would actually be *easier* to only use
sha1sum.rb and call it a day.  We just choose to support
SHA1 implementations provided by third parties if possible.

Performance is not a factor since sha1sum.rb performance is very
close to the C implementations.
Diffstat (limited to 't/bin')
-rwxr-xr-xt/bin/sha1sum.rb23
1 files changed, 23 insertions, 0 deletions
diff --git a/t/bin/sha1sum.rb b/t/bin/sha1sum.rb
new file mode 100755
index 0000000..b602e79
--- /dev/null
+++ b/t/bin/sha1sum.rb
@@ -0,0 +1,23 @@
+#!/usr/bin/env ruby
+# -*- encoding: binary -*-
+
+# Reads from stdin and outputs the SHA1 hex digest of the input this is
+# ONLY used as a last resort, our test code will try to use sha1sum(1),
+# openssl(1), or gsha1sum(1) before falling back to using this.  We try
+# all options first because we have a strong and healthy distrust of our
+# Ruby abilities in general, and *especially* when it comes to
+# understanding (and trusting the implementation of) Ruby 1.9 encoding.
+
+require 'digest/sha1'
+$stdout.sync = $stderr.sync = true
+$stdout.binmode
+$stdin.binmode
+bs = 16384
+digest = Digest::SHA1.new
+if buf = $stdin.read(bs)
+  begin
+    digest.update(buf)
+  end while $stdin.read(bs, buf)
+end
+
+$stdout.syswrite("#{digest.hexdigest}\n")