Created
March 15, 2012 17:12
-
-
Save stoicflame/2045351 to your computer and use it in GitHub Desktop.
Sample code for signing a client secret for FamilySearch oauth2 confidential client
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
KeyStore ks = KeyStore.getInstance("JKS"); | |
char[] password = "mypassword".toCharArray(); | |
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("mykeystore.jks"); | |
ks.load(is, password); | |
is.close(); | |
PrivateKey privateKey = (PrivateKey)ks.getKey("mykey", password); | |
// Create the secret with the private key | |
Security.addProvider(new BouncyCastleProvider()); | |
long timestamp = System.currentTimeMillis(); | |
String sTimestamp = Long.toString(timestamp); | |
byte[] bytesTimestampUtf8Unencrypted = sTimestamp.getBytes(Charset.forName("UTF-8")); | |
Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm()); | |
cipher.init(Cipher.ENCRYPT_MODE, privateKey); | |
byte[] bytesTimestampUtf8Encrypted = cipher.doFinal(bytesTimestampUtf8Unencrypted); | |
String encoded = (new BASE64Encoder()).encodeBuffer(bytesTimestampUtf8Encrypted); | |
String secret = URLEncoder.encode(encoded, "UTF-8"); | |
form.setClient_secret(secret); | |
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSSS"); | |
form.setClient_secret_time_stamp(formatter.format(new Date(timestamp))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment