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

View File

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

View File

@@ -7,6 +7,8 @@ import android.graphics.Rect;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.util.Log; import android.util.Log;
import org.briarproject.api.crypto.CryptoComponent;
/** /**
* Created by saiimons on 05/10/14. * Created by saiimons on 05/10/14.
*/ */
@@ -15,9 +17,14 @@ public class IdenticonDrawable extends Drawable {
private static final int CENTER_COLUMN_INDEX = 3; private static final int CENTER_COLUMN_INDEX = 3;
public IdenticonDrawable(String toShow) { public IdenticonDrawable(final CryptoComponent crypto, byte[] toShow) {
super(); super();
mDelegate = new IdenticonBase() { mDelegate = new IdenticonBase() {
@Override
protected CryptoComponent getCrypto() {
return crypto;
}
@Override @Override
protected int getRowCount() { protected int getRowCount() {
return 5; 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); getDelegate().show(input);
invalidate(); invalidate();
} }
public void show(String input) {
show(input.getBytes());
}
public void show(int input) { public void show(int input) {
show(String.valueOf(input)); show(String.valueOf(input));
} }
@@ -71,7 +75,7 @@ abstract public class IdenticonView extends View {
} }
public void show(byte input) { public void show(byte input) {
show(String.valueOf(input)); show(new byte[] { input });
} }
public void show(char input) { public void show(char input) {

View File

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