Groovy web console

subscribe to the feed Subscribe
to this
site
Hashed Token Signature for CosmosDB API (via #groovywebconsole)
tweet this snippet Tweet
this
script

Hashed Token Signature for CosmosDB API

Published 4 months ago by Lee Sobotkin
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;


String verb = "GET";
String rType = "dbs";
String rLink = "dbs/ToDoList";
String date = "Thu, 27 Apr 2017 00:51:12 GMT";
String master = "master";
String token = "1.0";
String key =  "dsZQi3KtZmCv1ljt3VNWNm7sQUF1y5rJfC6kv5JiwvW0EndXdDku/dkKBp8/ufDToSxLzR4y+O/0H/t4bQtVNw==";

String text = verb.toLowerCase() + "\n" + rType.toLowerCase() + "\n" + rLink + "\n" + date.toLowerCase() + "\n" + "" + "\n";
Mac mac = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKeySpec = new SecretKeySpec(key.decodeBase64(), "HmacSHA256");
mac.init(secretKeySpec);
byte[] digest = mac.doFinal(text.getBytes("UTF-8"));
String signature = digest.encodeBase64().toString();

String encodedURI = java.net.URLEncoder.encode("type=" + master + "&ver=" + token + "&sig=" + signature);