...
Before sending the request, the client will add a timestamp to the data in the request, so now the request will be something like:
Code Block |
---|
GET /esapis/v1.0/classlist?term=2015SP&subject=8.011×tamp=20140715113137 |
Then (also before the request is sent) to derive the hash value, the client will:
...
Code Block |
---|
import urllib2 import json import hashlib import datetime # The base URL: urlBase="https://mv-ezproxy-com.ezproxyberklee.flo.org/esapis/v1.0/classlist?term=2015SP&subject=8.011" # For authentication, as well as the base query parameters (term and subject), # the URL must include these additional query parameters: # user, timestamp, hash # # Form the hash variable (SHA-256 hash of base parameter values + timestamp + shared secret). user = "gravytrain" secret = "September" timeStamp = datetime.datetime.today().strftime('%Y%m%d%H%M%S') toBeHashed = "2015SP" + "8.011" + timeStamp + secret hashObj = hashlib.sha256(toBeHashed.encode()) # Complete the URL string by adding the additional parameters (timestamp, user, and hash): url = urlBase + "×tamp=" + timeStamp + "&user=" + user + "&hash=" + hashObj.hexdigest() # Get the data from the API in JSON form. result = json.load(urllib2.urlopen(url)) |
Some Other Languages/Environments
Other languages provide similar capability for generating SHA256 hash values. All the examples are generating hashes for a string with value "some value".
Java:
Code Block | ||
---|---|---|
| ||
import java.security.MessageDigest;
...
String textToBeHashed = "some value";
MessageDigest digest = MessageDigest.getInstance(args[1]);
byte[] hash = digest.digest(textToBeHashed.getBytes("UTF-8"));
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < hash.length; i++) {
String hex = Integer.toHexString(0xff & hash[i]);
if(hex.length() == 1) hexString.append('0');
hexString.append(hex);
} |
Bash Shell Command Line:
Code Block | ||
---|---|---|
| ||
echo -n "some value"|sha256sum |
Javascript:
Code Block | ||
---|---|---|
| ||
<script type="text/javascript" src="sjcl.min.js" ></script>
<script language="JavaScript" type="text/javascript">
var bitArray = sjcl.hash.sha256.hash("some value");
var hash = sjcl.codec.hex.fromBits(bitArray);
</script> |