Here are a few code samples showing the usage of the different modules from this package.
Note
You can find a program used to demo this package on this repo: https://github.com/svandegar/hashchain-demo
This script implements a hash chain, certifying data integrity in a database in a 7 lines of code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | from hashchain import records
from datetime import datetime
import pymongo
import random
# MongoDB connection
MONGO_CONNECTION_STRING = os.environ.get('MONGO_CONNECTION_STRING')
client = pymongo.MongoClient(MONGO_CONNECTION_STRING)
db = client['database-name']
collection = db.collection
# Build random data
data = {
'timestamp':datetime.now().replace(microsecond=0), # round to seconds to avoid problems due to MongoDB datetime precision limitation
'sensorId':'ERDP-QT24', # dummy sensorId
'value': random.random()
}
# Hash data along with the hash of the previous record
try:
last_record = collection.find({"sensorId": data['sensorId']}).sort([("timestamp", -1)])[0]
last_record_hash = last_record['hash']
except: # If this is the first record in the DB
last_record_hash = None
record = records.Record(data, last_record_hash)
# Save the dictionary output of the record in the database
collection.insert_one(record.to_dict())
|
This script verifies the integrity of an existing hash chain in a MongoDB database with two lines of code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | from hashchain import records
import pymongo
# MongoDB connection
MONGO_CONNECTION_STRING = os.environ.get('MONGO_CONNECTION_STRING')
client = pymongo.MongoClient(MONGO_CONNECTION_STRING)
db = client['database-name']
collection = db.collection
# Get the data form the DB
chain = mongo_collection.find({'sensorId': 'ERDP-QT24'},
{"_id": 0}).sort([("timestamp", -1)])
db_records = list(chain)
# Verify the chain validity
records.verify(db_records)
|