Modify IdenticonBase to use Briar's hashing and to use byte[] instead of String

This commit is contained in:
str4d
2016-01-21 02:36:21 +00:00
parent d645c435fd
commit dfb9222ccd
5 changed files with 51 additions and 17 deletions

View File

@@ -20,8 +20,15 @@ import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import org.briarproject.api.crypto.CryptoComponent;
import javax.inject.Inject;
import roboguice.RoboGuice;
public class AsymmetricIdenticon extends IdenticonView {
@Inject private CryptoComponent mCrypto;
private IdenticonBase mDelegate;
public AsymmetricIdenticon(Context context) {
@@ -45,7 +52,13 @@ public class AsymmetricIdenticon extends IdenticonView {
}
private void initDelegate() {
RoboGuice.injectMembers(getContext(), this);
mDelegate = new IdenticonBase() {
@Override
protected CryptoComponent getCrypto() {
return mCrypto;
}
@Override
protected int getRowCount() {
return 4;

View File

@@ -4,15 +4,13 @@ import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import java.security.MessageDigest;
import org.briarproject.api.crypto.CryptoComponent;
/**
* Created by saiimons on 05/10/14.
*/
public abstract class IdenticonBase {
private static final String HASH_ALGORITHM = "SHA-256";
private final CryptoComponent mCrypto;
private final int mRowCount;
private final int mColumnCount;
private final Paint mPaint;
@@ -23,6 +21,7 @@ public abstract class IdenticonBase {
private volatile boolean mReady;
public IdenticonBase() {
mCrypto = getCrypto();
mRowCount = getRowCount();
mColumnCount = getColumnCount();
mPaint = new Paint();
@@ -32,20 +31,16 @@ public abstract class IdenticonBase {
mPaint.setDither(true);
}
public static byte[] getHash(String input) {
public byte[] getHash(byte[] input) {
byte[] mHash;
// if the input was null
if (input == null) {
// we can't create a hash value and have nothing to show (draw to the view)
mHash = null;
}
// if the input was a proper string (non-null)
else {
// generate a hash from the string to get unique but deterministic byte values
} else {
// generate a hash from the input to get unique but deterministic byte values
try {
final MessageDigest digest = java.security.MessageDigest.getInstance(HASH_ALGORITHM);
digest.update(input == null ? new byte[0] : input.getBytes());
mHash = digest.digest();
mHash = mCrypto.hash(input);
} catch (Exception e) {
mHash = null;
}
@@ -68,9 +63,9 @@ public abstract class IdenticonBase {
}
}
public void show(String input) {
public void show(byte[] input) {
if(input != null) {
mHash = IdenticonBase.getHash(input);
mHash = getHash(input);
} else {
mHash = null;
}
@@ -89,6 +84,8 @@ public abstract class IdenticonBase {
}
}
abstract protected CryptoComponent getCrypto();
abstract protected int getRowCount();
abstract protected int getColumnCount();

View File

@@ -7,6 +7,8 @@ import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.Log;
import org.briarproject.api.crypto.CryptoComponent;
/**
* Created by saiimons on 05/10/14.
*/
@@ -15,9 +17,14 @@ public class IdenticonDrawable extends Drawable {
private static final int CENTER_COLUMN_INDEX = 3;
public IdenticonDrawable(String toShow) {
public IdenticonDrawable(final CryptoComponent crypto, byte[] toShow) {
super();
mDelegate = new IdenticonBase() {
@Override
protected CryptoComponent getCrypto() {
return crypto;
}
@Override
protected int getRowCount() {
return 5;

View File

@@ -49,11 +49,15 @@ abstract public class IdenticonView extends View {
}
}
public void show(String input) {
public void show(byte[] input) {
getDelegate().show(input);
invalidate();
}
public void show(String input) {
show(input.getBytes());
}
public void show(int input) {
show(String.valueOf(input));
}
@@ -71,7 +75,7 @@ abstract public class IdenticonView extends View {
}
public void show(byte input) {
show(String.valueOf(input));
show(new byte[] { input });
}
public void show(char input) {

View File

@@ -20,10 +20,17 @@ import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import org.briarproject.api.crypto.CryptoComponent;
import javax.inject.Inject;
import roboguice.RoboGuice;
public class SymmetricIdenticon extends IdenticonView {
private static final int CENTER_COLUMN_INDEX = 3;
@Inject private CryptoComponent mCrypto;
private IdenticonBase mDelegate;
public SymmetricIdenticon(Context context) {
@@ -42,7 +49,13 @@ public class SymmetricIdenticon extends IdenticonView {
}
private void initDelegate() {
RoboGuice.injectMembers(getContext(), this);
mDelegate = new IdenticonBase() {
@Override
protected CryptoComponent getCrypto() {
return mCrypto;
}
@Override
protected int getRowCount() {
return 5;