Forum Archive

Entropy builder finger dragging UI?

SimCityWok

How would one implement an entropy builder with a UI where you drag your finger (like Magic Text.py) until enough x,y coordinates are recorded to build a 256bit hash?
www.bitaddress.com does so with JavaScript but it's over my head.

Thanks!

ccc

https://forum.omz-software.com/topic/2988/dragging-finger-around-screen-to-build-entropy

cook

@SimCityWok
I didn't try this out but maybe here's a start

@Webmaster4o is very good with Javascript, maybe he can see exactly how they do it on that website.

import ui
import hashlib

#Here is a code example from 'http://pythoncentral.io/hashing-strings-with-python/' :

hash_object = hashlib.sha256(b'Hello World')
hex_dig = hash_object.hexdigest()
print hex_dig

class TouchHash(ui.View):
    def __init__(self):
        self.flex = 'WH'
        self.name = 'Swipe around to generate a hash'
        self.complete = False
        self.hash = ''
        self.textview = ui.TextView()
        self.textview.touch_enabled = False
        self.textview.editable = False
        self.textview.flex = 'WH'
        self.add_subview(self.textview)
        self.present()

    def do_hash_generation(self, location):
        #do what is necessary to generate your hash, when complete (100%) do self.complete=True
        #use the hashlib module???? (included and has documentation on pythonista)
        #I've never used this. If you want it like that website, my guess is that they string together coordinate information 
        #from the touches and then generate a hash from that. @Webmaster4o may be able to decipher what they are doing. He's pro with JS.

        self.hash = '' #update it
        self.textview.text = self.hash #show the text in the textview
        if self.complete:
            #print or return the hash
            self.close() #close the view

    def touch_began(self, touch):
        #touch.location provides a 2-tuple (x,y) coordinate.
        self.do_hash_generation(touch.location)

    def touch_moved(self, touch):
        self.do_hash_generation(touch.location)

    def touch_ended(self, touch):
        self.do_hash_generation(touch.location)

hash = TouchHash()
cook

Updated with @JonB 's suggestions from below.

import ui
import hashlib
import clipboard

class TouchHash(ui.View):
    def __init__(self):
        self.flex = 'WH'
        self.name = 'Swipe/Touch around to generate a hash'
        self.hash = hashlib.sha256()
        self.count = 0
        self.textview = ui.TextView()
        self.textview.touch_enabled = False
        self.textview.editable = False
        self.textview.flex = 'WH'
        self.add_subview(self.textview)
        self.present()

    def do_hash_generation(self, location, prev_location, timestamp):
        if self.count < 100:
            self.hash.update('{}{}{}{:15f}'.format(location[0],location[1],prev_location,timestamp))
            self.count += 1
            self.name = str(self.count) + '% complete'
            self.textview.text = 'Hash: ' + self.hash.hexdigest() #show the text in the textview
        elif self.count == 100:
            print self.hash.hexdigest()
            clipboard.set(self.hash.hexdigest())
            self.close() #close the view

    def touch_began(self, touch):
        self.do_hash_generation(touch.location, touch.prev_location, touch.timestamp)

    def touch_moved(self, touch):
        self.do_hash_generation(touch.location, touch.prev_location, touch.timestamp)

    def touch_ended(self, touch):
        #do nothing so that user can touch random spots
        pass

hash = TouchHash()
JonB

I think you want to use .update on your sha256 object each time the touch moves, Then call .digest to get the answer, maybe on touch ended.

Also, I would combine together location, prev_location, and the timestamp i.e something like

update('{}{}{:15f}'.format(t.location,t.prev_location,t.timestamp))

Webmaster4o

@cook That looks good! I also like @omz's idea from the other post of using device motion

SimCityWok

@ccc said:

https://forum.omz-software.com/topic/2988/dragging-finger-around-screen-to-build-entropy

I'd forgotten I'd asked that.

The answers this time are great though

SimCityWok

Is there a way to have a horizontal bar fill up as the percentage rises?

cook

@SimCityWok

Give this a try... Just learned a little bit about doing ui.Path the other day from @Webmaster4o (thanks again btw!). Maybe there's a better way actually accomplish this in code... Not sure!

Now it doesn't automatically close the view at the end. It gives a hud_alert and copies the hash to clipboard.

Because the view is presented without the title bar, you need to swipe down with two fingers to close it.

import ui
import hashlib
import clipboard
import console

class TouchHash(ui.View):
    def __init__(self):
        self.flex = 'WH'
        self.hash = hashlib.sha256()
        self.background_color = 1
        self.count = 0
        self.hash_label = ui.Label()
        self.hash_label.font = ('<system>', 8)
        self.hash_label.alignment = 1
        self.hash_label.text_color = 1
        self.pr_bar_w = 350
        self.add_subview(self.hash_label)
        self.present(hide_title_bar=True)

    def do_hash_generation(self, location, prev_location, timestamp):
        if self.count < 100:
            self.hash.update('{}{}{}{:15f}'.format(location[0],location[1],prev_location,timestamp))
            self.hash_label.text = self.hash.hexdigest() #show the text in the textview
            self.set_needs_display()
        elif self.count == 100:
            clipboard.set(self.hash.hexdigest())
            console.hud_alert('Hash on Clipboard')
        self.count += 1

    def touch_began(self, touch):
        if self.count < 101:
            self.do_hash_generation(touch.location, touch.prev_location, touch.timestamp)

    def touch_moved(self, touch):
        if self.count < 101:
            self.do_hash_generation(touch.location, touch.prev_location, touch.timestamp)

    def touch_ended(self, touch):
        #do nothing so that user can touch random spots
        pass

    def draw(self):
        self.cx, self.cy = self.center
        self.p_frame = ui.Path.rounded_rect(self.cx-self.pr_bar_w/2,self.cy-35/2,self.pr_bar_w,35, 5)
        ui.set_color(0.7)
        self.p_frame.stroke()
        self.p_bar = ui.Path.rounded_rect(self.cx-self.pr_bar_w/2,self.cy-35/2,self.pr_bar_w*(self.count/100.0),35, 5)
        ui.set_color('#dd6676')
        self.p_bar.fill()
        self.hash_label.frame = (self.cx-self.pr_bar_w/2, self.cy-35/2, self.pr_bar_w, 35)

hash = TouchHash()
SimCityWok

One final query....
If I wanted to implement this, how would I change the class code?

def make(bits=256):
    assert bits in (256, 512)
    hashes = []
    for hsh in range(int(bits//256)):
        myhash = TouchHash()
        hashes.append(myhash)
    return "".join(hashes)