Skip to content

Instantly share code, notes, and snippets.

@sharnie
Created September 10, 2019 17:33
Show Gist options
  • Save sharnie/cc6c46f3f78bcfb620984961e3e4b106 to your computer and use it in GitHub Desktop.
Save sharnie/cc6c46f3f78bcfb620984961e3e4b106 to your computer and use it in GitHub Desktop.
# BTW, the value here is not empty
public_key = File.read(@public_key_path)
data = params.permit!
# Get the p_signature parameter & base64 decode it.
signature = Base64.decode64(data['p_signature'])
# Remove the p_signature parameter
data.delete('p_signature')
# Ensure all the data fields are strings
data.each {|key, value| data[key] = String(value)}
# Sort the data
data_sorted = data.sort_by{|key, value| key}
# and serialize the fields
# serialization library is available here: https://github.com/jqr/php-serialize
data_serialized = PHP.serialize(data_sorted, true)
# verify the data
digest = OpenSSL::Digest::SHA1.new
pub_key = OpenSSL::PKey::RSA.new(public_key).public_key
verified = pub_key.verify(digest, signature, data_serialized)
# outputting false
verified #=> false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment