Last active
May 21, 2024 17:37
-
-
Save ifellows/0fab1a121d5212135f79242e2cb28702 to your computer and use it in GitHub Desktop.
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.fellstat.secureidentityencoder; | |
import android.content.DialogInterface; | |
import android.support.v7.app.AlertDialog; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
public class EncodeIdentity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_encode_identity); | |
final Button clickButton = (Button) findViewById(R.id.button); | |
final EditText key = (EditText) findViewById(R.id.key); | |
final EditText fn = (EditText) findViewById(R.id.firstname); | |
final EditText ln = (EditText) findViewById(R.id.lastname); | |
final TextView codeText = (TextView) findViewById(R.id.codetext); | |
final String salt = "1234567"; | |
clickButton.setOnClickListener( new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
String first = fn.getText().toString().toLowerCase().trim(); | |
String last = ln.getText().toString().toLowerCase().trim(); | |
int lastLength = Math.min(3,last.length()); | |
last = last.substring(0,lastLength); | |
if(first.length() == 0 || last.length() == 0){ | |
AlertDialog alertDialog = new AlertDialog.Builder(EncodeIdentity.this).create(); | |
alertDialog.setTitle("Alert"); | |
alertDialog.setMessage("first and last name may not be empty"); | |
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", | |
new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int which) { | |
dialog.dismiss(); | |
} | |
}); | |
alertDialog.show(); | |
key.setText("", TextView.BufferType.EDITABLE); | |
codeText.setText("", TextView.BufferType.EDITABLE); | |
return; | |
} | |
String code = first + ":" + last; | |
String hash = hash(code,salt); | |
hash = hash.substring(0, Math.min(9,hash.length())); | |
key.setText(hash, TextView.BufferType.EDITABLE); | |
codeText.setText(code, TextView.BufferType.EDITABLE); | |
} | |
}); | |
} | |
//http://howtodoinjava.com/security/how-to-generate-secure-password-hash-md5-sha-pbkdf2-bcrypt-examples/ | |
public static String hash(String passwordToHash, String salt) | |
{ | |
String generatedPassword = null; | |
try { | |
MessageDigest md = MessageDigest.getInstance("SHA-512"); | |
md.update(salt.getBytes()); | |
byte[] bytes = md.digest(passwordToHash.getBytes()); | |
StringBuilder sb = new StringBuilder(); | |
for(int i=0; i< bytes.length ;i++) | |
{ | |
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1)); | |
} | |
generatedPassword = sb.toString(); | |
} | |
catch (NoSuchAlgorithmException e) | |
{ | |
e.printStackTrace(); | |
return null; | |
} | |
return generatedPassword; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment