Created
March 24, 2016 07:09
-
-
Save Delamare2112/ca3db76aa7968fd2abba to your computer and use it in GitHub Desktop.
A great way to both generate and recieve random data
This file contains 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
package com.terrasect.entropygenerator; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
import java.nio.charset.Charset; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.util.Calendar; | |
import android.content.Context; | |
import android.os.Environment; | |
import android.util.Log; | |
import com.google.common.io.Files; | |
/** | |
* Created by Trevor on 10/6/15. | |
*/ | |
public final class EntropyManager { | |
private EntropyManager() {} // Keep people from creating instances | |
private static int entropyCounter; | |
private static String entropy; | |
private static Context context; | |
private static File entropyFile; | |
private static File counterFile; | |
final private static String TAG = "EntropyManager"; | |
public static void Initialize(Context context) { | |
EntropyManager.context = context; | |
entropyFile = new File(context.getFilesDir() + File.separator + "entropy.data"); | |
counterFile = new File(context.getFilesDir() + File.separator + "count.data"); | |
try { | |
if(!counterFile.exists()) { | |
counterFile.createNewFile(); | |
Files.write("0", counterFile, Charset.defaultCharset()); | |
} | |
entropyCounter = Integer.parseInt(Files.toString(counterFile, Charset.defaultCharset())); | |
entropy = Files.toString(entropyFile, Charset.defaultCharset()); | |
AddEntropy(entropy); | |
} | |
catch (FileNotFoundException e) { | |
Log.e(TAG, "File not found: " + e.toString()); | |
} | |
catch (IOException e) { | |
Log.e("Exception", "Could not read from file: " + e.toString()); | |
} | |
} | |
public static void AddEntropy(String data) { | |
entropyCounter++; | |
entropy = Hash(data + entropy + Calendar.getInstance().getTime()); | |
try { | |
Files.write(Integer.toString(entropyCounter), counterFile, Charset.defaultCharset()); | |
Files.write(entropy, entropyFile, Charset.defaultCharset()); | |
} catch (IOException e) { | |
Log.e("Exception", "Could not write to file: " + e.toString()); | |
} | |
} | |
public static String GetRawEntropy() { | |
AddEntropy(entropy); | |
return entropy; | |
} | |
private static String Hash(String data) { | |
try { | |
byte[] result = MessageDigest.getInstance("SHA256").digest(data.getBytes()); | |
StringBuilder stringBuffer = new StringBuilder(); | |
for (byte b : result) | |
stringBuffer.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1)); | |
return stringBuffer.toString(); | |
} catch (NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
return ""; | |
} | |
} | |
public static int GetCount() { | |
return entropyCounter; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment