Personal tools

Ace:StorePython

From Adapt

Revision as of 17:17, 17 December 2010 by Toaster (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The following is some sample code for reading and parsing ACE Token Stores in Python. All samples were tested on RHEL5 against python 2.4.3 with a backport of the hashlib library installed.

Read Token Store

The simpliest way (with little-no error checking) to read a well-formed token store.

def readHeader(file):
    currLine = file.readline()
    if not currLine:
        return False
    headerParts = string.split(currLine)
    if (len(headerParts) != 6):
        return False
    return headerParts

def readIdentifiers(infile):
    line = infile.readline().rstrip("\n")
    ids = []
    while line != "":
        ids.append(line)        
        line = infile.readline().rstrip("\n")
    return ids

def readProof(infile):
    line = infile.readline().rstrip("\n")
    proof = []
    while line != "":
        proof.append(line)        
        line = infile.readline().rstrip("\n")
    return proof

infile = open("/YOUR_TOKEN_STORE_FILE",'rb')

while 1:
    header = readHeader(infile)
    if not header:
        break
    identifiers = readIdentifiers(infile)
    proof = readProof(infile)

infile.close()

Calculate a file's proof

def calculateLevel(lowerHash,rowString):
    hashAlg = hashlib.sha256()    
    for hash in string.split(rowString,":"):
        if (hash == "X"):
            hashAlg.update(lowerHash)
        else:
            hashAlg.update(binascii.a2b_hex(hash))
    return hashAlg.digest()

...
...
# using proof lines from above, compute a files's proof
# Note, sha256 hardcoded in this example, store algorithm should be checked
digFile = open("/FILE_TO_VALIDATE,'rb')
hashAlg = hashlib.sha256()
hashAlg.update(digFile.read())
prevhash = hashAlg.digest()

for proofLine in proof:
    prevhash = calculateLevel(prevhash,proofLine)
print "Proof Result: " + binascii.b2a_hex(prevhash)

This example will read the FILE_TO_VALIDATE, calculate a digest, then feed that digest into each level of the digest. The final level's digest should be compared to the address of that round stored on the IMS.