Skip to content

Instantly share code, notes, and snippets.

@nderkach
Created December 18, 2014 11:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save nderkach/d11540e9af322f1c1c74 to your computer and use it in GitHub Desktop.
Save nderkach/d11540e9af322f1c1c74 to your computer and use it in GitHub Desktop.
Slightly modified source code to calculate X-CS-Url-Signature
import java.util.*;
import java.lang.*;
import java.io.*;
import java.net.*;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Locale;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
@SuppressWarnings("all")
public class GetURL
{
private static class Preconditions
{
public static <T> T a(final T t) {
if (t == null) {
throw new NullPointerException();
}
return t;
}
public static void a() {
}
public static void a(final boolean b, final Object o) {
if (!b) {
throw new IllegalArgumentException(String.valueOf(o));
}
}
}
private interface TypedOutput
{
String fileName();
long length();
String mimeType();
void writeTo(OutputStream p0) throws java.io.IOException;
}
private class JsonTypedOutput implements TypedOutput
{
public final byte[] a;
private final String b;
JsonTypedOutput(final byte[] a, final String s) {
super();
this.a = a;
this.b = "application/json; charset=" + s;
}
@Override
public String fileName() {
return null;
}
@Override
public long length() {
return this.a.length;
}
@Override
public String mimeType() {
return this.b;
}
@Override
public void writeTo(final OutputStream outputStream) throws java.io.IOException {
outputStream.write(this.a);
}
}
private final class Header
{
private final String name;
private final String value;
public Header(final String name, final String value) {
super();
this.name = name;
this.value = value;
}
@Override
public boolean equals(final Object o) {
if (this != o) {
if (o == null || this.getClass() != o.getClass()) {
return false;
}
final Header header = (Header)o;
Label_0059: {
if (this.name != null) {
if (this.name.equals(header.name)) {
break Label_0059;
}
}
else if (header.name == null) {
break Label_0059;
}
return false;
}
if (this.value != null) {
if (this.value.equals(header.value)) {
return true;
}
}
else if (header.value == null) {
return true;
}
return false;
}
return true;
}
public String getName() {
return this.name;
}
public String getValue() {
return this.value;
}
@Override
public int hashCode() {
int hashCode;
if (this.name != null) {
hashCode = this.name.hashCode();
}
else {
hashCode = 0;
}
final int n = hashCode * 31;
final String value = this.value;
int hashCode2 = 0;
if (value != null) {
hashCode2 = this.value.hashCode();
}
return n + hashCode2;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
String name;
if (this.name != null) {
name = this.name;
}
else {
name = "";
}
final StringBuilder append = sb.append(name).append(": ");
String value;
if (this.value != null) {
value = this.value;
}
else {
value = "";
}
return append.append(value).toString();
}
}
private final class Request
{
private final TypedOutput body;
private final List<Header> headers;
private final String method;
private final String url;
public Request(final String method, final String url, final List<Header> list, final TypedOutput body) {
super();
if (method == null) {
throw new NullPointerException("Method must not be null.");
}
if (url == null) {
throw new NullPointerException("URL must not be null.");
}
this.method = method;
this.url = url;
if (list == null) {
this.headers = Collections.emptyList();
}
else {
this.headers = Collections.unmodifiableList((List<? extends Header>)new ArrayList<Header>(list));
}
this.body = body;
}
public TypedOutput getBody() {
return this.body;
}
public List<Header> getHeaders() {
return this.headers;
}
public String getMethod() {
return this.method;
}
public String getUrl() {
return this.url;
}
}
private static final class EncUtils
{
public static String a(final byte[] array) {
return String.format(Locale.US, "%040x ", new BigInteger(1, array));
}
public static byte[] a(final byte[] array, final String s) {
try {
final Mac instance = Mac.getInstance("HmacSHA1");
instance.init(new SecretKeySpec(s.getBytes(), "HmacSHA1"));
return instance.doFinal(array);
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
private class RetrofitHttpClient
{
private final String a;
private String b;
public RetrofitHttpClient(final String a) {
this.a = a;
}
private Request a(final Request request) throws java.net.MalformedURLException, UnsupportedEncodingException {
final byte[] b = this.b(request.getUrl());
byte[] a;
if (request.getBody() != null && request.getBody() instanceof JsonTypedOutput) {
System.out.println("body");
a = this.a(b, ((JsonTypedOutput)request.getBody()).a);
}
else {
a = b;
}
String s;
if (this.b == null) {
s = this.a;
}
else {
s = this.a + "." + this.b;
}
final String a2 = EncUtils.a(EncUtils.a(a, s));
System.out.println(a2);
final ArrayList<Header> list = new ArrayList<Header>(request.getHeaders());
list.add(new Header("X-CS-Url-Signature", a2));
return new Request(request.getMethod(), request.getUrl(), list, request.getBody());
}
private byte[] a(final byte[] array, final byte[] array2) {
final byte[] array3 = new byte[array.length + array2.length];
System.arraycopy(array, 0, array3, 0, array.length);
System.arraycopy(array2, 0, array3, array.length, array2.length);
return array3;
}
private byte[] b(final String s) throws java.net.MalformedURLException, UnsupportedEncodingException {
return s.substring(s.indexOf(new URL(s).getPath())).getBytes("UTF-8");
}
public void a(final String b) {
this.b = b;
}
}
private static final class RetrofitUtils
{
public static String a(final List<Header> list, final String s) {
boolean b = true;
Preconditions.a(list != null && b, "Headers argument shouldn't be null");
if (s == null) {
b = false;
}
Preconditions.a(b, "HeaderName argument shouldn't be null");
for (final Header header : list) {
if (s.equals(header.getName())) {
return header.getValue();
}
}
return null;
}
}
private String getSignature() throws java.net.MalformedURLException, UnsupportedEncodingException, java.io.IOException {
String data = new String("{\"actionType\":\"manual_login\",\"credentials\":{\"authToken\":\"qwerty\",\"email\":\"nzoakhvi@sharklasers.com\"}}");
List<Header> headers = Arrays.asList(
new Header("Accept-Encoding", "gzip, deflate"),
new Header("Content-Type", "application/json; charset=utf-8"),
new Header("User-Agent", "Couchsurfing/48 (iPhone; iOS 8.1.1; Scale/2.00) Couchsurfing/ios/48/3.0/"),
);
Request basicRequest = new Request("POST", "https://hapi.couchsurfing.com/api/v2/sessions", null, new JsonTypedOutput(data.getBytes("UTF-8"), "UTF-8"));
// Request basicRequest = new Request("GET", "https://hapi.couchsurfing.com/api/v2/users/1003669205", headers, null);
RetrofitHttpClient client = new RetrofitHttpClient("v3#!R3v44y3ZsJykkb$E@CG#XreXeGCh");
Request localRequest = client.a(basicRequest);
System.out.println(localRequest.getHeaders().size());
// localRequest.getBody().writeTo(System.out);
String str = RetrofitUtils.a(localRequest.getHeaders(), "X-CS-Url-Signature");
return str;
}
public static void main (String[] args) throws java.lang.Exception, java.net.MalformedURLException
{
GetURL getURL = new GetURL();
System.out.println(getURL.getSignature());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment