Ejemplo n.º 1
0
def checkCertificateHashInMerkleTree():
    time.sleep(3)
    try:
        r_id = request.form['r_id']
        cursor = db.certificates.find({
            "r_id": r_id,
            "isHashed": "1"
        }, {"_id": 0})
        records = list(cursor)
        merkle_root = records[0]["receipt"]["merkleRoot"]
        proof = records[0]["receipt"]["proof"]
        certificate = records[0]["certificate"]
        target_hash = records[0]["receipt"]["targetHash"]

        #options = {'algorithm': 'URDNA2015', 'format': 'application/nquads'}
        cert_json = str(certificate)
        target_hash1 = sha256(cert_json)

        print target_hash
        print target_hash1

        if not proof:
            # no siblings, single item tree, so the hash should also be the root
            if target_hash == merkle_root:
                return "1"
            else:
                return "0"

        #target_hash = get_buffer(target_hash)
        #merkle_root = get_buffer(merkle_root)

        proof_hash = target_hash
        for x in proof:
            if 'left' in x:
                # then the sibling is a left node
                proof_hash = sha256(x['left'] + proof_hash)
            elif 'right' in x:
                # then the sibling is a right node
                proof_hash = sha256(proof_hash + x['right'])
            else:
                # no left or right designation exists, proof is invalid
                return "0"
        #print proof_hash
        #print merkle_root
        if hexlify(proof_hash) == hexlify(merkle_root):
            return "1"
        else:
            return "1"

    except Exception, e:
        return "0"
Ejemplo n.º 2
0
 def do_hash_certificate(self, certificate):
     cert_utf8 = certificate.decode('utf-8')
     cert_json = json.loads(cert_utf8)
     normalized = jsonld.normalize(cert_json, {'algorithm': 'URDNA2015', 'format': 'application/nquads'})
     hashed = sha256(normalized)
     self.tree.add_leaf(hashed, False)
     return hashed
Ejemplo n.º 3
0
 def do_execute(self, state):
     normalized = jsonld.normalize(state.certificate_json, {
         'algorithm': 'URDNA2015',
         'format': 'application/nquads'
     })
     hashed = sha256(normalized)
     state.local_hash = hashed
     return True
Ejemplo n.º 4
0
def mergeCertificate():
    try:
        r_ids = request.form['r_ids']
        ids = r_ids.split(',', -1)
        inq = []
        for id in ids:
            inq.append(id)
        cursor = db.certificates.find({"r_id": {"$in": inq}}, {"_id": 0})
        records = list(cursor)
        #rjon = json.dumps(records)
        #print rjon
        for rjson in records:
            #options = {'algorithm': 'URDNA2015', 'format': 'application/nquads'}
            #cert_utf8 = str(rjson["certificate"]).decode('utf-8')
            #print cert_utf8
            #cert_json = json.loads(cert_utf8)
            #print cert_json
            #rjson["isHashed"] = "1"
            #rjson["r_id"] = random_str(32)
            #cert_json = json.dumps(rjson["certificate"])

            #cert_utf8 = certificate.decode('utf-8')
            cert_json = str(rjson["certificate"])
            #print cert_json;
            #normalized = jsonld.normalize(cert_json, options=options)
            hashed = sha256(cert_json)

            #print rjson
            #print normalized
            tree.add_leaf(hashed, False)
            #db.certificates.insert_one(rjson)

        tree.make_tree()
        #db.certificates.remove({"r_id":{"$in":inq}})

        #global j
        #j = 0
        #for rj in records:
        #  receipt = tree.make_receipt(j,"wait2replace")
        #  rj["receipt"] = receipt
        #  print rj
        #  global j
        #  j = j + 1

        #certificate = json.loads(docu)
        #db.certificates.insert_one(certificate)
        #cursor = db.stu_certifactes.find({'r_id':r_id},{"_id":0})

        #records = list(cursor)
        #temp = records[0]
        #temp["approved"] = "1"
        #rjon = json.dumps(temp)
        #re = json.loads(rjon)
        #db.stu_certifactes.remove({'r_id':r_id})
        #db.stu_certifactes.insert_one(re)
        return tree.get_merkle_root()
    except Exception, e:
        return "0"
Ejemplo n.º 5
0
 def do_execute(self, state):
     options = {
         'algorithm': 'URDNA2015',
         'format': 'application/nquads',
         'documentLoader': cached_document_loader
     }
     normalized = jsonld.normalize(state.certificate_json, options=options)
     hashed = sha256(normalized)
     state.local_hash = hashed
     return True
Ejemplo n.º 6
0
def do_hash_certificate(certificate):
    """
      Hash the JSON-LD normalized certificate
      :param certificate:
      :return:
      """
    options = {'algorithm': 'URDNA2015', 'format': 'application/nquads'}
    cert_utf8 = certificate.decode('utf-8')
    cert_json = json.loads(cert_utf8)
    normalized = jsonld.normalize(cert_json, options=options)
    hashed = sha256(normalized)
    #self.tree.add_leaf(hashed, False)
    return hashed
Ejemplo n.º 7
0
def generatedCertificate():
    try:
        r_id = request.form['r_id']
        txid = request.form['txid']
        cursor = db.records.find_one({"r_id": r_id}, {"_id": 0})
        certids = cursor["certids"]

        ids = certids.split(',', -1)
        inq = []
        for id in ids:
            inq.append(id)
        cursor1 = db.certificates.find({"r_id": {"$in": inq}}, {"_id": 0})
        print cursor1
        #db.certificates.remove({"isHashed":"1"})
        records1 = list(cursor1)
        #rjon = json.dumps(records1)
        #print rjon
        for rjson in records1:
            #options = {'algorithm': 'URDNA2015', 'format': 'application/nquads'}
            #cert_utf8 = str(rjson["certificate"]).decode('utf-8')
            #print cert_utf8
            #cert_json = json.loads(cert_utf8)
            #print cert_json
            #rjson["isHashed"] = "1"

            #cert_json = json.dumps(rjson["certificate"])
            #print cert_json
            #cert_utf8 = certificate.decode('utf-8')
            cert_json = str(rjson["certificate"])
            #print cert_json
            #print cert_json;
            #normalized = jsonld.normalize(cert_json, options=options)
            #print normalized
            hashed = sha256(cert_json)
            #print hashed
            #print normalized
            tree.add_leaf(hashed, False)
            #db.certificates.insert_one(rjson)
        tree.make_tree()

        db.certificates.remove({"r_id": {"$in": inq}})

        global j
        j = 0
        for rj in records1:
            receipt = tree.make_receipt(j, txid)
            rj["receipt"] = receipt
            rj["isHashed"] = "1"
            #rj["r_id"] = random_str(32)
            j = j + 1
            print rj
            db.certificates.insert_one(rj)

        doc = {
            "is_send": "1",
            "rawtran": cursor["rawtran"],
            "merkleRootHex": cursor["merkleRootHex"],
            "user1": cursor["user1"],
            "user2": cursor["user2"],
            "user3": cursor["user3"],
            "r_id": cursor["r_id"],
            "certids": cursor["certids"],
            "state": cursor["state"]
        }
        db.records.remove({"r_id": r_id})
        print doc
        db.records.insert_one(doc)
        print "success"

        #certificate = json.loads(docu)
        #db.certificates.insert_one(certificate)
        #cursor = db.stu_certifactes.find({'r_id':r_id},{"_id":0})

        #records = list(cursor)
        #temp = records[0]
        #temp["approved"] = "1"
        #rjon = json.dumps(temp)
        #re = json.loads(rjon)
        #db.stu_certifactes.remove({'r_id':r_id})
        #db.stu_certifactes.insert_one(re)
        #return tree.get_merkle_root();
        return "1"
    except Exception, e:
        return "0"