aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/theb/ssh/TouchEntropy.java
blob: 33ad49980a14619c4296cf06c7078f3a3e2e51fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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;
        }
    }
}