# HG changeset patch # User Alessio Caiazza # Date 1264875011 -3600 # Node ID 58bc392e043d88964ec859866d7022c89c65a3a3 # Parent 9d98ed26d4d004b3b811dc82f68dcea89b890b91 tests. Porta dell'utility modhex diff -r 9d98ed26d4d004b3b811dc82f68dcea89b890b91 -r 58bc392e043d88964ec859866d7022c89c65a3a3 Rakefile --- a/Rakefile Sat Jan 30 16:18:43 2010 +0100 +++ b/Rakefile Sat Jan 30 19:10:11 2010 +0100 @@ -14,7 +14,7 @@ s.files = FileList["{tests,lib,docs,ext}/**/*"].exclude("rdoc").to_a s.require_paths = ['lib', 'ext'] s.autorequire = 'yubiruby' - #s.test_file = 'tests/ts_yubiruby.rb' + s.test_file = 'tests/ts_yubiruby.rb' s.has_rdoc = true s.extra_rdoc_files = ['README'] s.extensions = ['ext/extconf.rb'] @@ -22,4 +22,20 @@ Rake::GemPackageTask.new(spec) do |pkg| pkg.need_tar = true +end + +task :compile do + spec.extensions.each do |e| + file = File.basename e + dir = File.dirname e + %x{cd #{dir}; ruby #{file}; make} + end +end + +task :test => :compile do +# %w{ lib ext tests }.each do |dir| +# path = File.expand_path(File.join(File.dirname(__FILE__), dir)) +# $LOAD_PATH.unshift(path) +# end + require spec.test_file end \ No newline at end of file diff -r 9d98ed26d4d004b3b811dc82f68dcea89b890b91 -r 58bc392e043d88964ec859866d7022c89c65a3a3 bin/modhex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bin/modhex Sat Jan 30 19:10:11 2010 +0100 @@ -0,0 +1,48 @@ +#!/usr/bin/env ruby -wKU +$:.unshift File.join(File.dirname(__FILE__),'..','lib') +$:.unshift File.join(File.dirname(__FILE__),'..','ext') + +require 'yubiruby' + +if __FILE__ == $PROGRAM_NAME + if ARGV.length < 1 + msg=<<-EOF +Usage: #{$PROGRAM_NAME} [-dh] + +Convert input DATA as specified and print output to STDOUT. + + -d: Decode data (the default is to encode data). + -h: Use hex encoding for all non-modhex data. + DATA: string with data to encode + +Examples: + + ModHex encode ASCII-string \"test\": + #{$PROGRAM_NAME} test + + Decode ModHex data \"ifhgieif\" into ASCII string: + #{$PROGRAM_NAME} -d ifhgieif + + ModHex encode hex-encoded data \"b565716f\": + #{$PROGRAM_NAME} -h b565716f + + Decode ModHex data \"nghgibhv\" and print hex-encode data: + #{$PROGRAM_NAME} -d -h nghgibhv +EOF + abort msg + end + + hex = ARGV.include? "-h" + decode = ARGV.include? "-d" + data = ARGV.last + + if decode + out = data.modhex_decode + out = YubiRuby::HEX.encode(out) if hex + puts out + else + data = YubiRuby::HEX.decode(data) if hex + puts data.modhex_encode + end + +end \ No newline at end of file diff -r 9d98ed26d4d004b3b811dc82f68dcea89b890b91 -r 58bc392e043d88964ec859866d7022c89c65a3a3 ext/libyubikey.c --- a/ext/libyubikey.c Sat Jan 30 16:18:43 2010 +0100 +++ b/ext/libyubikey.c Sat Jan 30 19:10:11 2010 +0100 @@ -76,6 +76,7 @@ rb_define_method(mModHex, MODHEX_DECODE, modhex_decode, 0); rb_define_singleton_method(mModHex, MODHEX_VALID, modhex_sing_is_modhex, 1); rb_define_method(mModHex, MODHEX_VALID, modhex_is_modhex, 0); + rb_define_const(mModHex, "MODHEX_MAP", rb_str_new2(YUBIKEY_MODHEX_MAP)); //extends String with ModHex rb_include_module(rb_cString, mModHex); diff -r 9d98ed26d4d004b3b811dc82f68dcea89b890b91 -r 58bc392e043d88964ec859866d7022c89c65a3a3 lib/hex.rb --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/hex.rb Sat Jan 30 19:10:11 2010 +0100 @@ -0,0 +1,30 @@ +module YubiRuby + module HEX + def self.encode( obj ) + s = obj.to_str + s.unpack('U'*s.length).collect {|x| x.to_s 16}.join + end + + def hex_encode + HEX.encode(self) + end + + def self.decode( obj ) + s = obj.to_str + dec = "" + if (s.length % 2 == 0) + (s.length/2).times { |i| dec << s[i*2,2].hex.chr } + end + + return dec + end + + def hex_decode + HEX.decode(self) + end + end +end + +#class String +# include YubiRuby::HEX +#end diff -r 9d98ed26d4d004b3b811dc82f68dcea89b890b91 -r 58bc392e043d88964ec859866d7022c89c65a3a3 lib/yubiruby.rb --- a/lib/yubiruby.rb Sat Jan 30 16:18:43 2010 +0100 +++ b/lib/yubiruby.rb Sat Jan 30 19:10:11 2010 +0100 @@ -1,4 +1,5 @@ require 'libyubikey' +require 'hex' module YubiRuby # C code version diff -r 9d98ed26d4d004b3b811dc82f68dcea89b890b91 -r 58bc392e043d88964ec859866d7022c89c65a3a3 tests/tc_modhex.rb --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/tc_modhex.rb Sat Jan 30 19:10:11 2010 +0100 @@ -0,0 +1,13 @@ +require "test/unit" + +require "libyubikey" + +class TestLibyubikey < Test::Unit::TestCase + def test_string + str = "pippo234strd" + assert_equal(enc = str.modhex_encode, YubiRuby::ModHex.encode(str)) + assert_equal(enc.modhex_decode, YubiRuby::ModHex.decode(enc)) + assert_equal(str, str.modhex_encode().modhex_decode()) + assert(enc.modhex?) + end +end \ No newline at end of file diff -r 9d98ed26d4d004b3b811dc82f68dcea89b890b91 -r 58bc392e043d88964ec859866d7022c89c65a3a3 tests/ts_yubiruby.rb --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/ts_yubiruby.rb Sat Jan 30 19:10:11 2010 +0100 @@ -0,0 +1,6 @@ +$:.unshift File.join(File.dirname(__FILE__),'..','lib') +$:.unshift File.join(File.dirname(__FILE__),'..','ext') + +require "test/unit" + +require "tc_modhex"