Advertisement
jan10101

Kraken Java API Client

Jun 29th, 2014
2,934
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.32 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.OutputStreamWriter;
  5. import java.io.UnsupportedEncodingException;
  6. import java.net.HttpURLConnection;
  7. import java.net.URL;
  8. import java.security.InvalidKeyException;
  9. import java.security.MessageDigest;
  10. import java.security.NoSuchAlgorithmException;
  11.  
  12. import javax.crypto.Mac;
  13. import javax.crypto.spec.SecretKeySpec;
  14.  
  15. import org.apache.commons.codec.binary.Base64;
  16.  
  17. public class KrakenClient {
  18.  
  19.     protected static String key = "myAPIKey";     // API key
  20.     protected static String secret = "MySecret====";  // API secret
  21.     protected static String url = "api.kraken.com";     // API base URL
  22.     protected static String version = "0"; // API version
  23.  
  24.  
  25.     public static void main(String[] args) throws Exception {
  26.         queryPrivateMethod("Balance");
  27.     }
  28.  
  29.     public static void queryPrivateMethod(String method) throws NoSuchAlgorithmException, IOException{
  30.  
  31.         long nonce = System.currentTimeMillis();
  32.  
  33.         String path = "/" + version + "/private/" + method; // The path like "/0/private/Balance"
  34.  
  35.         String urlComp = "https://"+url+path; // The complete url like "https://api.kraken.com/0/private/Balance"
  36.  
  37.         String postdata = "nonce="+nonce;
  38.  
  39.         String sign = createSignature(nonce, path, postdata);
  40.  
  41.         postConnection(urlComp, sign, postdata);
  42.     }
  43.  
  44.     /**
  45.      * @param nonce
  46.      * @param path
  47.      * @param postdata
  48.      * @return
  49.      * @throws NoSuchAlgorithmException
  50.      * @throws IOException
  51.      */
  52.     private static String createSignature(long nonce, String path,
  53.             String postdata) throws NoSuchAlgorithmException, IOException {
  54.  
  55.         return hmac(path+sha256(nonce + postdata),  new String(Base64.decodeBase64(secret)));
  56.     }
  57.  
  58.     public static String sha256Hex(String text) throws NoSuchAlgorithmException, IOException{
  59.         return org.apache.commons.codec.digest.DigestUtils.sha256Hex(text);
  60.     }
  61.  
  62.     public static byte[] sha256(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException{
  63.         MessageDigest md = MessageDigest.getInstance("SHA-256");
  64.  
  65.         md.update(text.getBytes());
  66.         byte[] digest = md.digest();
  67.  
  68.         return digest;
  69.     }
  70.  
  71.     public static void postConnection(String url1, String sign, String postData) throws IOException{
  72.  
  73.         URL url = new URL( url1 );
  74.         HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  75.  
  76.         connection.addRequestProperty("API-Key", key);
  77.         connection.addRequestProperty("API-Sign", Base64.encodeBase64String(sign.getBytes()));
  78.         //      connection.addRequestProperty("API-Sign", sign);
  79.         connection.addRequestProperty("User-Agent", "Mozilla/4.0");
  80.         connection.setRequestMethod( "POST" );
  81.         connection.setDoInput( true );
  82.         connection.setDoOutput( true );
  83.         connection.setUseCaches( false );
  84.         //      connection.setRequestProperty( "Content-Type",
  85.         //              "application/x-www-form-urlencoded" );
  86.         connection.setRequestProperty( "Content-Length", String.valueOf(postData.length()) );
  87.  
  88.         OutputStreamWriter writer = new OutputStreamWriter( connection.getOutputStream() );
  89.         writer.write( postData );
  90.         writer.flush();
  91.  
  92.  
  93.         BufferedReader reader = new BufferedReader(
  94.                 new InputStreamReader(connection.getInputStream()) );
  95.  
  96.         for ( String line; (line = reader.readLine()) != null; )
  97.         {
  98.             System.out.println( line );
  99.         }
  100.  
  101.         writer.close();
  102.         reader.close();
  103.     }
  104.  
  105.  
  106.     public static String hmac(String text, String secret){
  107.  
  108.         Mac mac =null;
  109.         SecretKeySpec key = null;
  110.  
  111.         // Create a new secret key
  112.         try {
  113.             key = new SecretKeySpec( secret.getBytes( "UTF-8"), "HmacSHA512" );
  114.         } catch( UnsupportedEncodingException uee) {
  115.             System.err.println( "Unsupported encoding exception: " + uee.toString());
  116.             return null;
  117.         }
  118.         // Create a new mac
  119.         try {
  120.             mac = Mac.getInstance( "HmacSHA512" );
  121.         } catch( NoSuchAlgorithmException nsae) {
  122.             System.err.println( "No such algorithm exception: " + nsae.toString());
  123.             return null;
  124.         }
  125.  
  126.         // Init mac with key.
  127.         try {
  128.             mac.init( key);
  129.         } catch( InvalidKeyException ike) {
  130.             System.err.println( "Invalid key exception: " + ike.toString());
  131.             return null;
  132.         }
  133.  
  134.  
  135.         // Encode the text with the secret
  136.         try {
  137.  
  138.             return new String( mac.doFinal(text.getBytes( "UTF-8")));
  139.         } catch( UnsupportedEncodingException uee) {
  140.             System.err.println( "Unsupported encoding exception: " + uee.toString());
  141.             return null;
  142.         }
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement