This repository has been archived by the owner on Nov 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Android Master Key Generation
Angelina edited this page Dec 13, 2017
·
1 revision
/**
* Copyright 2017 Idealnaya rabota LLC
* Licensed under Multy.io license.
* See LICENSE for details
*/
package io.multy.masterkeygeneration;
import android.annotation.SuppressLint;
import android.content.Context;
import android.provider.Settings;
import com.google.android.gms.iid.InstanceID;
import org.spongycastle.jcajce.provider.digest.SHA3;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class MasterKeyGenerator {
private static byte[] getInstanceID(Context context) {
return InstanceID.getInstance(context).getId().getBytes();
}
@SuppressLint("HardwareIds")
private static byte[] getSecureID(Context context) {
return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID).getBytes();
}
/**
* Provide PIN or password if user set it. Method will be used after alpha release
* for better secure protection, but it returns empty value for now.
*
* @return zero if no password or PIN, otherwise bytes of password or PIN.
*/
private static byte[] getUserSecret() {
return new byte[0];
}
public static byte[] generateKey(Context context) {
ByteArrayOutputStream outputStream = null;
try {
outputStream = new ByteArrayOutputStream();
outputStream.write(getInstanceID(context));
outputStream.write(getUserSecret());
outputStream.write(getSecureID(context));
return calculateSHA3256(outputStream.toByteArray());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
private static byte[] calculateSHA3256(byte[] input){
SHA3.DigestSHA3 digestSHA3 = new SHA3.Digest256();
return digestSHA3.digest(input);
}
}