Advertisement
Guest User

Untitled

a guest
May 12th, 2016
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.71 KB | None | 0 0
  1. package com.uservoice;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.security.InvalidAlgorithmParameterException;
  8. import java.security.InvalidKeyException;
  9. import java.security.NoSuchAlgorithmException;
  10.  
  11. import javax.crypto.Cipher;
  12. import javax.crypto.CipherOutputStream;
  13. import javax.crypto.NoSuchPaddingException;
  14. import javax.crypto.spec.IvParameterSpec;
  15. import javax.crypto.spec.SecretKeySpec;
  16.  
  17. import org.apache.commons.codec.net.URLCodec;
  18. import org.apache.commons.codec.binary.Base64;
  19. import org.apache.commons.codec.digest.DigestUtils;
  20. import org.json.JSONObject;
  21.  
  22. /**
  23.  * Singleton class for creating a UserVoice SSO Token. See {@link #main(String[])} for usage example.
  24.  * Requires commons-codec library {@link http://commons.apache.org/codec/}.
  25.  */
  26. public class TokenGenerator {
  27.   /* If you're acme.uservoice.com then this value would be 'acme' */
  28.   private static final String USERVOICE_SUBDOMAIN = "FILL IN";
  29.  
  30.   /* Get this from your UserVoice General Settings page */
  31.   private static final String SSO_KEY = "FILL IN";    
  32.   private static final byte[] INIT_VECTOR = "OpenSSL for Ruby".getBytes();  
  33.   private SecretKeySpec secretKeySpec;
  34.   private IvParameterSpec ivSpec;
  35.   private URLCodec urlCodec = new URLCodec("ASCII");
  36.   private Base64 base64 = new Base64();
  37.   private static TokenGenerator INSTANCE = new TokenGenerator();
  38.  
  39.   public static TokenGenerator getInstance() {
  40.     if (INSTANCE == null) {
  41.       INSTANCE = new TokenGenerator();
  42.     }
  43.     return INSTANCE;
  44.   }
  45.  
  46.   private TokenGenerator() {
  47.     String salted = SSO_KEY + USERVOICE_SUBDOMAIN;
  48.     byte[] hash = DigestUtils.sha(salted);
  49.     byte[] saltedHash = new byte[16];
  50.     System.arraycopy(hash, 0, saltedHash, 0, 16);
  51.  
  52.     secretKeySpec = new SecretKeySpec(saltedHash, "AES");
  53.     ivSpec = new IvParameterSpec(INIT_VECTOR);
  54.   }
  55.  
  56.   private void encrypt(InputStream in, OutputStream out) throws Exception {
  57.     try {
  58.       byte[] buf = new byte[1024];
  59.      
  60.       Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  61.       cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivSpec);
  62.      
  63.       out = new CipherOutputStream(out, cipher);
  64.      
  65.       int numRead = 0;
  66.       while ((numRead = in.read(buf)) >= 0) {
  67.         out.write(buf, 0, numRead);
  68.       }
  69.       out.close();
  70.     } catch (InvalidKeyException e) {
  71.       e.printStackTrace();
  72.     } catch (NoSuchAlgorithmException e) {
  73.       e.printStackTrace();
  74.     } catch (NoSuchPaddingException e) {
  75.       e.printStackTrace();
  76.     } catch (InvalidAlgorithmParameterException e) {
  77.       e.printStackTrace();
  78.     } catch (java.io.IOException e) {
  79.       e.printStackTrace();
  80.     }
  81.   }
  82.  
  83.   public String create(JSONObject json) throws Exception {
  84.     byte[] data = json.toString().getBytes();
  85.  
  86.     ByteArrayOutputStream out = new ByteArrayOutputStream();
  87.     for (int i = 0; i < 16; i++) {
  88.       data[i] ^= INIT_VECTOR[i];
  89.     }
  90.     encrypt(new ByteArrayInputStream(data), out);
  91.  
  92.     String token = new String(urlCodec.encode(base64.encode(out.toByteArray())));
  93.     return token;
  94.   }    
  95.  
  96.   public static void main(String[] args) {
  97.     try {
  98.       JSONObject jsonObj = new JSONObject();
  99.      
  100.       jsonObj.put("guid", "1234");
  101.       jsonObj.put("expires", "2009-05-18 17:24:28");
  102.       jsonObj.put("display_name", "Richard White");
  103.       jsonObj.put("email", "[email protected]");
  104.       jsonObj.put("url", "http://acme.com/users/1234");
  105.       jsonObj.put("avatar_url", "http://acme.com/users/1234/avatar.png");
  106.      
  107.       System.out.println( TokenGenerator.getInstance().create(jsonObj) );
  108.     } catch (Exception e) {
  109.       e.printStackTrace();
  110.     }
  111.   }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement