Skip to content

Instantly share code, notes, and snippets.

@devkhan
Created June 12, 2015 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devkhan/abdc59de5278b447c80f to your computer and use it in GitHub Desktop.
Save devkhan/abdc59de5278b447c80f to your computer and use it in GitHub Desktop.
This is a test class done as an assignment for as part of the tasks given by AppAmplify team to me.
import java.io.*; // For Input-Output.
import java.security.MessageDigest; // For encryption.
import java.security.NoSuchAlgorithmException; // Algorithm Exception.
import java.util.Formatter; // Formatting generated key.
import java.util.Random; // Generating random appsflyerkey.
public class task
{
// appsflyerkey - private static member.
private static String appsflyerkey;
// alphabet - Character to create random alphanumeric string from.
private static final String alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static void main(String[] args)
{
Console console = System.console();
// For checking if proper input is available or not.
boolean flag=false;
while(!flag)
{
appsflyerkey = console.readLine("Enter appsflyerkey (or leave empty for random appsflyerkey): ");
if (appsflyerkey.length()>0 && appsflyerkey.length()!=16)
{
System.out.println("Error!!! Please either enter no key or a valid key of 16 characters. Retry.");
}
else
{
// If user doesn't enter any string, a random string is generated.
Random random = new Random();
StringBuilder sb = new StringBuilder(16);
for( int i = 0; i < 16; i++ )
sb.append(alphabet.charAt(random.nextInt(alphabet.length())));
appsflyerkey = sb.toString();
System.out.println("Generating random appsflyerkey: " + appsflyerkey);
flag=true;
}
}
// dkh generated from a substring of appsflyerkey.
String dkh = appsflyerkey.substring(0,8);
// timestamp generated at runtime.
String timestamp = new java.text.SimpleDateFormat("ddMMyyyyhhmmss").format(new java.util.Date());
// uid generated from appsflyerkey and timestamp.
String uid = appsflyerkey+timestamp;
// af_v generated according to decompiled code.
String af_v = a(appsflyerkey.substring(0, 7) + (dkh + timestamp.substring(timestamp.length() - 7)));
System.out.println("Encrypted kay i.e. 'af_v' : " + af_v);
}
/**
* Generates an encypted key using a given string.
* This method is based on the logic of the decompiled code. I have only renamed variable names as they were causing conflicts.
* @param paramString [Given string]
* @return [Encrypted key.]
*/
public static String a(String paramString) {
int i = 0;
Formatter formatter;
try {
MessageDigest localObject = MessageDigest.getInstance("SHA-1");
((MessageDigest)localObject).reset();
((MessageDigest)localObject).update(paramString.getBytes("UTF-8"));
byte[] bytes = localObject.digest();
formatter = new Formatter();
int j = bytes.length;
while (i < j) {
formatter.format("%02x", new Object[] { Byte.valueOf(bytes[i]) });
i += 1;
}
String formatString = formatter.toString();
formatter.close();
return (String)formatString;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment