Last active
December 5, 2016 00:13
-
-
Save anklos/ee3958e036987b8a1afe1caf81117f67 to your computer and use it in GitHub Desktop.
openssl sha256 signature
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'openssl' | |
require "base64" | |
DELIMITER = ":cba_signature_delimiter:" | |
# Airtasker sends encoded data | |
key = OpenSSL::PKey::RSA.new(File.read('private.pem')) | |
timestamp = '31480574670' # unix time stamp in utc timezone | |
signature = key.sign(OpenSSL::Digest::SHA256.new, timestamp) | |
data = timestamp + DELIMITER + Base64.strict_encode64(signature) | |
encoded_data = Base64.strict_encode64(data) | |
puts encoded_data | |
# CBA decode the encoded data and verify | |
decoded_data = Base64.decode64(encoded_data) | |
decoded_timestamp, encoded_signature = decoded_data.split(DELIMITER) | |
decoded_signature = Base64.decode64(encoded_signature) | |
pubkey = OpenSSL::PKey::RSA.new(File.read('public.pem')) | |
if pubkey.verify(OpenSSL::Digest::SHA256.new, decoded_signature, decoded_timestamp) | |
puts 'the signature is valid' | |
else | |
puts 'the signature is invalid' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment