onlyforbopi
2/23/2018 - 12:24 PM

PYTHON CHECKSUMS

  1. Luhn Algorithm (Convert CA to RF)
  2. Mod10 Checksum (Convert POD to KHP)
def mod10(number):
    '''
    Function: mod10(input)
    Category: Checksums
    Name: mod10
    Input: numerical integer / string
    Output: check digit, target number, logical check, full product
    Description: Implements a mod 10 check digit algorithm to a string
    Usage: print(mod10(string))
    Notes: Iterates digits in reverse
    
    '''   
    # Initialize an array to hold the digits
    digits = []
    # For even check
    even = False
    # Counter - For even check on indexes.
    i = 0
    
    # Iterate over digits in reverse order
    for digit in reversed(number):
        # parse digit    
        digit = ord(digit) - ord('0')
        #print("Checkign digit: " + str(digit))
        if (i == 0) or ( i%2==0):
            #print( str(digit) + 'is even')
            digit = digit * 2
            #print ("newdigit: " + str(digit))
            if digit >= 10:
                digit = digit % 10 + digit // 10
        digits.append(digit)
        #print(digits)
        even = not even
        
        wanted_number = sum(digits)
         
        i += 1
    
    # Initialize check to find number
    check = 0
    
    # Iterate until you find it.
    while check < 10:
        if ((wanted_number + check) % 10) == 0:
            break
        else:
            check += 1
            
        
    return (digit, wanted_number, check, ( (sum(digits) + check) % 10 == 0), str(number) + str(check))

    
    
    
    
########


    
def KHPtoPOD(string1):
    '''
    Function: Convert KHP (Kodikos E. Pliromis) to POD (Paroxi)
    Name: KHPtoPOD
    Input: string1
    Output: string
    Usage: KHPtoPOD(string1)
    Notes: Converts KHP to POD by changing the check digit in the end.
    '''
    
    # Initialize counters, outputs, and parse arguments
    outstring = ''
    status = 0
    string1 = str(string1)
    
    # splice substrings
    area_code = string1[:1]
    eight_dig = string1[1:9]
    diadoxos = string1[9:11]
    checkdigitold = string1[11]
    
    # Calculate modulos
    fixed_int = int(eight_dig)
    mod_in = fixed_int % 11
    
    if mod_in == 10:
        mod_in = 1
        
    outstring = str(area_code) + str(eight_dig) + str(diadoxos) + str(mod_in)
    
    return outstring  
    
    
##########


def contoRF(inte, kod, psi):
    '''
    Function: Converts Contract Account to RF
    Name: contoRF
    Input: Contract Account, 5 digit code, single digit check digit
    Output: Full RF Code without Sum Total. 
    Usage: contoRF(inte, kod, psi)
    Notes: Converts Contract Account to RF using mod97
    '''
    
    
    base=str(inte)
    
    if len(base) == 12:
        baseaug = str(kod) + str(psi) + "000" + base + "271500"
    else:
        baseaug = str(kod) + str(psi) + "00000" + base + "271500"
    
    baseaugn=int(baseaug)
    
    cd=baseaugn%97
    
    cdfixed= 98 - cd
    
    if cdfixed < 10:
        cdfixed = "0" + str(cdfixed)
    else:
        cdfixed = str(cdfixed)
    
    if len(base) == 12:
        output = "RF" + str(cdfixed) + str(kod) + str(psi) + "000" + str(inte)
    else:
        output = "RF" + str(cdfixed) + str(kod) + str(psi) + "00000" + str(inte)
    
    # Validating its the correct calc
    #print ("Validating")
    temp2 = str(output[4:])
    #print (temp2)
    temp3 = temp2 + "2715" + str(cdfixed)
    #print (temp3)
    
    
    if int(temp3)%97 == 1:
        status = "ok"
    else:
        status = "notok"
        
    return (output, status)
def KHPtoPOD(string1):
    
    outstring = ''
    status = 0
    string1 = str(string1)
    
    area_code = string1[:1]
    eight_dig = string1[1:9]
    diadoxos = string1[9:11]
    checkdigitold = string1[11]
    
    fixed_int = int(eight_dig)
    mod_in = fixed_int % 11
    
    if mod_in == 10:
        mod_in = 1
        
    outstring = str(area_code) + str(eight_dig) + str(diadoxos) + str(mod_in)
    return outstring   
 
    
def contoRF(inte, kod, psi):
    #print(inte)
    #print(type(inte))
    base=str(inte)
    #print (base)
    #print (type(base))
    if len(base) == 12:
        baseaug = str(kod) + str(psi) + "000" + base + "271500"
    else:
        baseaug = str(kod) + str(psi) + "00000" + base + "271500"
    #print (baseaug)
    #print (type(baseaug))
    baseaugn=int(baseaug)
    #print (baseaugn)
    #print (type(baseaugn))
    cd=baseaugn%97
    #print (cd)
    #print (type(cd))
    cdfixed= 98 - cd
    #print (cdfixed)
    if cdfixed < 10:
        cdfixed = "0" + str(cdfixed)
    else:
        cdfixed = str(cdfixed)
    
    if len(base) == 12:
        output = "RF" + str(cdfixed) + str(kod) + str(psi) + "000" + str(inte)
    else:
        output = "RF" + str(cdfixed) + str(kod) + str(psi) + "00000" + str(inte)
    
    # Validating its the correct calc
    #print ("Validating")
    temp2 = str(output[4:])
    #print (temp2)
    temp3 = temp2 + "2715" + str(cdfixed)
    #print (temp3)
    
    
    if int(temp3)%97 == 1:
        status = "ok"
    else:
        status = "notok"
        
    return (output, status)
def KHPtoPOD(string1):
    
    outstring = ''
    status = 0
    string1 = str(string1)
    
    area_code = string1[:1]
    eight_dig = string1[1:9]
    diadoxos = string1[9:11]
    checkdigitold = string1[11]
    
    fixed_int = int(eight_dig)
    mod_in = fixed_int % 11
    
    if mod_in == 10:
        mod_in = 1
        
    outstring = str(area_code) + str(fixed_int) + str(diadoxos) + str(mod_in)
    return outstring
    
print(KHPtoPOD(282010001011))
def convertPODtoKHP(string):
    
    # Cast into string
    string = str(string)
    
    # split first 11 elements
    proc_string = string[:11]
    check = proc_string[:7]
    check2 = proc_string[:1]
    
    #print (check)
    #print (check2)
    
    out_string = ''
    status = 0
    
    # Cast back into integer
    if check == "VERMION":
        out_string = proc_string
        #print ("PROBLEM: VERMION IN POD")
        status = 1
    elif check2 == "&":
        out_string = proc_string
        #print ("PROBLEM: & IN POD")
        status = 2
    else:
        fixed_int = int(proc_string)
        mod_in = fixed_int % 11
    
        if mod_in == 10:
            mod_in = 1
    
        out_string = str(proc_string) + str(mod_in)
    
    return (out_string, status)
def contoRF(inte, kod, psi):
  '''
  Function: contoRF
  Description: Transforms CA to RF
  Usage: contoRF(string, "90775", 8)
  Example:
  Notes:
  '''
  
  
  
    #print(inte)
    #print(type(inte))
    base=str(inte)
    #print (base)
    #print (type(base))
    if len(base) == 12:
        baseaug = str(kod) + str(psi) + "000" + base + "271500"
    else:
        baseaug = str(kod) + str(psi) + "00000" + base + "271500"
    #print (baseaug)
    #print (type(baseaug))
    baseaugn=int(baseaug)
    #print (baseaugn)
    #print (type(baseaugn))
    cd=baseaugn%97
    #print (cd)
    #print (type(cd))
    cdfixed= 98 - cd
    #print (cdfixed)
    if cdfixed < 10:
        cdfixed = "0" + str(cdfixed)
    else:
        cdfixed = str(cdfixed)
    
    if len(base) == 12:
        output = "RF" + str(cdfixed) + str(kod) + str(psi) + "000" + str(inte)
    else:
        output = "RF" + str(cdfixed) + str(kod) + str(psi) + "00000" + str(inte)
    
    # Validating its the correct calc
    #print ("Validating")
    temp2 = str(output[4:])
    #print (temp2)
    temp3 = temp2 + "2715" + str(cdfixed)
    #print (temp3)
    
    
    if int(temp3)%97 == 1:
        status = "ok"
    else:
        status = "notok"
        
    return (output, status)