# https://httpd.apache.org/docs/2.4/misc/password_encryptions.html # https://www.askapache.com/online-tools/htpasswd-generator/ from tarpncalctg.custom import CustomBase from tarpncalctg import api import base64 import hashlib _ = api.t class htpasswd_sha1(CustomBase): stack = None def __init__(self, stack): self.stack = stack # __init__ def help(self): msg = "

htpasswd_sha1

" msg += "

Takes r0 (password) from the stack

" msg += "

Takes r1 (username) from the stack

" msg += "

Calculates the htpasswd_sha1

" msg += "

Pushes the result to r0

" api.show_popup('Custom function', msg) # If you want to implement translated texts, you can define the texts in customFunctions.translations/htpasswd_sha1/*.yml # and then reference them using the _(textKey) function: # api.show_popup('Custom function', _('taRpnCalcTG.CustomFunctions.htpasswd_sha1.btnHLP')) # help def calculate(self): if self.stack.inputMode != 'TEXT': api.toast('This operation is only supported in TEXT mode') # api.toast(_('taRpnCalcTG.CustomFunctions.htpasswd_sha1.onlySupportedInDEC')) return if self.stack.finish_entry() == -1: return # abort here because data in input line is invalid if self.stack.size() < 2: api.toast('This operation needs 2 argument') # api.toast(_('taRpnCalcTG.CustomFunctions.htpasswd_sha1.argumentsNeeded', count=1)) return # do the calculation and return result to the stack r0 = str(self.stack.get_register(0)) r1 = str(self.stack.get_register(1)) m = hashlib.sha1() m.update(bytes(r0, 'utf-8')) hashbytes = m.digest() b64 = base64.standard_b64encode(hashbytes) result = r1 + ":{SHA}" + format(b64)[2:-1] self.stack.pop(2) self.stack.push(result) # calculate # htpasswd_sha1