require File.expand_path(File.dirname(__FILE__) + '/spec_helper') require 'dionysus/security/string' describe String do it "should generate a salt 16 bytes long" do String.salt.length.should == 16 end it "should generate a salt arbitrary nubmer of bytes long" do String.salt(5).length.should == 5 String.salt(1).length.should == 1 end it "should calculate sha512 in base64" do digest = "KHErTuI/5urCF3vi6yTDxvAHBydW1CN3zVfBWDP/ItbmgeVi5IFq47ew0hPHzyK3AuMDh+Z9MBBlf3/mACgVMQ==" "abcde\240z405".sha512.should == digest "abcde\240z405".sha512(:encoding => :base64).should == digest end it "should calculate sha256/sha2 in base64" do digest = "vGeWpAMcqj2XuE8P4FirA2oUt1S78c6puQk03EOdnQY=" "abcde\240z405".sha256.should == digest "abcde\240z405".sha256(:encoding => :base64).should == digest "abcde\240z405".sha2.should == digest "abcde\240z405".sha2(:encoding => :base64).should == digest end it "should calculate sha1 in base64" do digest = "wxeCFXPVXePFcpwuFDjonyn1G/w=" "abcde\240z405".sha1.should == digest "abcde\240z405".sha1(:encoding => :base64).should == digest end it "should calculate md5 in base64" do digest = "ThT0y7KbAinjLpuChqsRuQ==" "abcde\240z405".md5.should == digest "abcde\240z405".md5(:encoding => :base64).should == digest end it "should calculate sha512 with a salt" do salt = 'abcdefg' digest = "oxoD5Mwq6ck4B1P2tfsrCQnNHzBYQRKOFGTj1T0EFPWeNuy1/YPtsKGiz2os9ljt0/OwXEt/l8LsK/ktbwEHbg==" "abcde\240z405".sha512(:salt => salt).should == digest end it "should calculate sha256/sha2 with a salt" do salt = 'abcdefg' digest = "fWzU2g2GFAS5EAgTS09djo2eDDQdNp1xSTdrRO7m9v4=" "abcde\240z405".sha256(:salt => salt).should == digest "abcde\240z405".sha2(:salt => salt).should == digest end it "should calculate sha1 with a salt" do salt = 'abcdefg' digest = "JFdwlGl341qtbqX6mPQbjs8d1/U=" "abcde\240z405".sha1(:salt => salt).should == digest end it "should calculate md5 with a salt" do salt = 'abcdefg' digest = "m59029ys/bc2HpeuZqcQ3w==" "abcde\240z405".md5(:salt => salt).should == digest end it "should calculate sha512 in hex/hexidecimal" do digest = "28712b4ee23fe6eac2177be2eb24c3c6f007072756d42377cd57c15833ff22d6e681e562e4816ae3b7b0d213c7cf22b702e30387e67d3010657f7fe600281531" "abcde\240z405".sha512(:encoding => :hex).should == digest "abcde\240z405".sha512(:encoding => :hexidecimal).should == digest end it "should calculate sha256/sha2 in hex/hexidecimal" do digest = "bc6796a4031caa3d97b84f0fe058ab036a14b754bbf1cea9b90934dc439d9d06" "abcde\240z405".sha256(:encoding => :hex).should == digest "abcde\240z405".sha2(:encoding => :hex).should == digest "abcde\240z405".sha256(:encoding => :hexidecimal).should == digest "abcde\240z405".sha2(:encoding => :hexidecimal).should == digest end it "should calculate sha1 in hex/hexidecimal" do digest = "c317821573d55de3c5729c2e1438e89f29f51bfc" "abcde\240z405".sha1(:encoding => :hex).should == digest "abcde\240z405".sha1(:encoding => :hexidecimal).should == digest end it "should calculate md5 in hex/hexidecimal" do digest = "4e14f4cbb29b0229e32e9b8286ab11b9" "abcde\240z405".md5(:encoding => :hex).should == digest "abcde\240z405".md5(:encoding => :hexidecimal).should == digest end it "should calculate sha512 in binary" do digest = "(q+N\342?\346\352\302\027{\342\353$\303\306\360\a\a'V\324#w\315W\301X3\377\"\326\346\201\345b\344\201j\343\267\260\322\023\307\317\"\267\002\343\003\207\346}0\020e\177\177\346\000(\0251" "abcde\240z405".sha512(:encoding => :binary).should == digest end it "should calculate sha256/sha2 in binary" do digest = "\274g\226\244\003\034\252=\227\270O\017\340X\253\003j\024\267T\273\361\316\251\271\t4\334C\235\235\006" "abcde\240z405".sha256(:encoding => :binary).should == digest "abcde\240z405".sha2(:encoding => :binary).should == digest end it "should calculate sha1 in binary" do digest = "\303\027\202\025s\325]\343\305r\234.\0248\350\237)\365\e\374" "abcde\240z405".sha1(:encoding => :binary).should == digest end it "should calculate md5 in binary" do digest = "N\024\364\313\262\233\002)\343.\233\202\206\253\021\271" "abcde\240z405".md5(:encoding => :binary).should == digest end end