lib/nsisam/client.rb in nsisam-0.2.8 vs lib/nsisam/client.rb in nsisam-0.3.0
- old
+ new
@@ -1,7 +1,8 @@
require "json"
require "net/http"
+require "digest/sha1"
require File.dirname(__FILE__) + '/errors'
module NSISam
class Client
@@ -54,14 +55,16 @@
# * "date" [String] the date when the value was stored
# * "data" [String, Hash, Array] the data stored at that key
# @raise [NSISam::Errors::Client::KeyNotFoundError] When the key doesn't exists
# @example
# nsisam.get("some key")
- def get(key)
+ def get(key, expected_checksum=nil)
request_data = {:key => key}.to_json
request = prepare_request :GET, request_data
- execute_request(request)
+ response = execute_request(request)
+ verify_checksum(response["data"], expected_checksum) unless expected_checksum.nil?
+ response
end
# Update data stored at a given SAM key
#
# @param [String] key of the data to update
@@ -93,7 +96,13 @@
http.request(request)
end
raise NSISam::Errors::Client::KeyNotFoundError if response.code == "404"
JSON.parse(response.body)
end
+
+ def verify_checksum(data, expected_checksum)
+ sha1_checksum = Digest::SHA1.hexdigest(data)
+ raise NSISam::Errors::Client::ChecksumMismatchError unless sha1_checksum == expected_checksum
+ end
+
end
end