From 95fcd722943eff110febe2625f5a2118c34c6112 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Sat, 6 Aug 2011 00:00:07 +0200 Subject: add JCryption::digest and specs --- lib/ayk/jcryption.rb | 21 ++++++++++++++++++++- spec/jcryption_spec.rb | 4 ++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/ayk/jcryption.rb b/lib/ayk/jcryption.rb index 3b4871f..d498184 100644 --- a/lib/ayk/jcryption.rb +++ b/lib/ayk/jcryption.rb @@ -25,6 +25,8 @@ #---------------------------------------------------------------------------- # require 'ayk/maths' +require 'digest/md5' +require 'digest/sha2' # module JCryption # @@ -47,5 +49,22 @@ module JCryption OpenSSL::PKey::RSA.generate( bits ) end module_function :gen_keypair + # + def digest passwd, salt=nil + n = passwd.length + i = ((n/2)+1)-n%8 + i=0 if i<0 + if salt.nil? + m = Digest::MD5.new + m << Time.now.to_s + n = Time.now.to_i%20 + salt = m.to_s[n..n+12] + end + d = Digest::SHA2.new << passwd[0..i]+salt+passwd[i+1..-1] + [d,salt] + end + module_function :digest + # end - +# +# EOF diff --git a/spec/jcryption_spec.rb b/spec/jcryption_spec.rb index ce1d174..85901de 100644 --- a/spec/jcryption_spec.rb +++ b/spec/jcryption_spec.rb @@ -15,6 +15,10 @@ describe JCryption do h['passwd'].should eql "my_password" h['timestamp'].should eql "1252937955" end + it 'should digest salted passwd' do + d,s = JCryption::digest 'mylovelypasswd' + JCryption::digest('mylovelypasswd',s)[0].should == d + end end end # -- cgit v1.1-2-g2b99