Skip to content

Instantly share code, notes, and snippets.

@AAlvarez90
Last active April 21, 2016 20:43
Show Gist options
  • Save AAlvarez90/0611bab8ad4793d1f4d103644867f902 to your computer and use it in GitHub Desktop.
Save AAlvarez90/0611bab8ad4793d1f4d103644867f902 to your computer and use it in GitHub Desktop.
P_SHA1 for Ruby
#Testing function PHA1:
require 'base64'
require 'openssl'
#Sample Data:
secret = Base64.decode64("kAIthWJeZ3UoS4k6qLIbfA==")
label = "WS-SecureConversationWS-SecureConversation"
seed = Base64.decode64("yP9lu4OUF1ah534Re/ZfcQ==")
new_seed = label + seed
#puts Base64.encode64(new_seed)
length = 24
def p_sha1(client_secret, server_secret, size_bytes = 32)
digest = OpenSSL::Digest.new('sha1')
hmac_key = client_secret
i = 0
b1 = server_secret
psha = []
while i < size_bytes do
b1 = OpenSSL::HMAC.digest(digest,hmac_key, b1)
b2 = b1+ server_secret
temp = OpenSSL::HMAC.digest(digest,hmac_key,b2 )
j = 0
while j < temp.length do
if i < size_bytes
psha[i] = temp[j]
i+=1
else
break
end
j+=1
end
end
return psha.join('')
end
psha1 = p_sha1(secret, new_seed, length)
puts Base64.encode64(psha1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment