diff options
Diffstat (limited to 'src/org/theb/ssh/TouchEntropy.java')
-rw-r--r-- | src/org/theb/ssh/TouchEntropy.java | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/org/theb/ssh/TouchEntropy.java b/src/org/theb/ssh/TouchEntropy.java new file mode 100644 index 0000000..33ad499 --- /dev/null +++ b/src/org/theb/ssh/TouchEntropy.java @@ -0,0 +1,86 @@ +package org.theb.ssh; + +import android.app.Activity; +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.Typeface; +import android.graphics.Paint.FontMetrics; +import android.os.Bundle; +import android.util.Log; +import android.view.MotionEvent; +import android.view.View; + +public class TouchEntropy extends Activity { + protected String mEntropy; + + @Override + protected void onCreate(Bundle icicle) { + super.onCreate(icicle); + + mEntropy = new String(); + setContentView(new TouchView(this)); + } + + class TouchView extends View { + Paint mPaint; + FontMetrics mFontMetrics; + + private boolean mFlipFlop = false; + private long mLastTime = 0; + + public TouchView(Context c) { + super(c); + + mPaint = new Paint(); + mPaint.setAntiAlias(true); + mPaint.setTypeface(Typeface.DEFAULT); + mPaint.setTextAlign(Paint.Align.CENTER); + mPaint.setTextSize(16); + mPaint.setColor(Color.WHITE); + mFontMetrics = mPaint.getFontMetrics(); + } + + @Override + public void onDraw(Canvas c) { + String prompt = getResources().getString(R.string.prompt_touch); + c.drawText(prompt + " " + (int)(100.0 * (mEntropy.length() / 20.0)) + "% done", + getWidth() / 2, + getHeight() / 2 - (mFontMetrics.ascent + mFontMetrics.descent) / 2, + mPaint); + } + + @Override + public boolean onMotionEvent(MotionEvent event) { + // Only get entropy every 200 milliseconds to ensure the user has moved around. + long now = System.currentTimeMillis(); + if ((now - mLastTime) < 200) + return true; + else + mLastTime = now; + + Log.d("SSH", "Last time was " + mLastTime); + + + // Get the lowest 4 bits of each X, Y input and concat to the entropy-gathering + // string. + if (mFlipFlop) + mEntropy += (byte)(((int)event.getX() & 0xF0) | ((int)event.getY() & 0x0F)); + else + mEntropy += (byte)(((int)event.getY() & 0xF0) | ((int)event.getX() & 0x0F)); + + mFlipFlop = !mFlipFlop; + + // SHA1PRNG only keeps 20 bytes (160 bits) of entropy. + if (mEntropy.length() > 20) { + TouchEntropy.this.setResult(RESULT_OK, mEntropy); + TouchEntropy.this.finish(); + } + + invalidate(); + + return true; + } + } +} |