1
votes

How do I convert string or a value to a byte array? I need it for SOAP authentication. My requirement is to - On the client side, this is how to create a digest: 1. Client Side Digest Array = byte array nonce + byte array UTC date-time of UTF-8 string + byte array UTF-8 plain text password (concatenate these three). 2. Client Side SHA-1 Digest = Hash with SHA-1 algorithm the Client Side Digest Array. 3. Client Side WS-Security Digest = 64-bit encode the Client Side SHA-1 Digest

Password_Digest = Base64 ( SHA-1 ( nonce + timestamp + password ) )

This is the code I am using to generate nonce, timestamp and digest_password. User password is a string. Some ting is wrong in the whole process and my digest is not successfully generated. I guess I have these data types right, byte array and UTF8 is confusing me. I added utf8 conversion but no difference.

def nonce
   chars = ("a".."z").to_a + ("1".."9").to_a + ("A".."Z").to_a
   @nonce = Array.new(20, '').collect{chars[rand(chars.size)]}.join
end

def timestamp
    t = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S.%LZ")
    @timestamp = t.to_s
end

def digest_password

ic = Iconv.new('UTF-8//IGNORE', 'US-ASCII')
$time =  ic.iconv( timestamp + ' ')[0..-2]
$pass =  ic.iconv( password + ' ')[0..-2]
temp = (nonce.bytes.to_a + $time.bytes.to_a + $pass.bytes.to_a)
@digest_password = Base64.strict_encode64(Digest::SHA1.hexdigest(temp.to_s))

###  temp =  Digest::SHA1.hexdigest(nonce + timestamp + password) ##old    
###@digest_password = Base64.encode64(temp) ##old

end


    <env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:wsdl="http://xml.myserver.com/ok/service/v1_5" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
  <env:Header>
       <wsse:Security env:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
       <wsse:UsernameToken wsu:Id="UsernameToken-1" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:Username>user</wsse:Username>
       <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">YWIwM2QyZWI3YTEwMTAzZmNkNmZiNmEwMjg1ODlkOTU0OTNmNmUxYQ==
       </wsse:Password> 
       <wsse:Nonce>ZEUyQ2J6bmw5cjdDZmt1QjVqTjQ=</wsse:Nonce>
       <wsu:Created>2012-03-27T11:08:35.125Z</wsu:Created>
</wsse:UsernameToken>
2

2 Answers

1
votes

Finally was able to solve this issue.

Password_Digest = Base64 ( SHA-1 ( nonce + create + password) )

nonce = nonce as string. Before Base64 endoce, E.g "1234"

create = time as string. No encoding

password = password as string. No encoding.

Base64Nonce = Base64.encode64(nonce).strip #Base64 encode of "1234"

chars = ("a".."z").to_a + ("1".."9").to_a + ("A".."Z").to_a
nonce = Array.new(20, '').collect{chars[rand(chars.size)]}.join

t = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S.%LZ")

$time = t
$pass = p
Base64Nonce = Base64.encode64(nonce).strip 

$digest_pass = Base64.encode64(Digest::SHA1.digest(nonce + $time + $pass)).strip



          "wsse:Username" => username,
          "wsse:Password" => $digest_pass,
          "wsse:Nonce" => Base64Nonce,
          "wsu:Created" => $time,
0
votes

Is it right to Base64.encode in the nonce method and then Base64.encode again in the digest_password method? Seems strange for me. Maybe you should do

def nonce
   chars = ("a".."z").to_a + ("1".."9").to_a + ("A".."Z").to_a
   @nonce = Array.new(20, '').collect{chars[rand(chars.size)]}.join
end