mcard-js
Version:
A JavaScript implementation of MCard - A data model for persistently storing content with cryptographic hashing and timestamping
132 lines (122 loc) • 5.35 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.UPGRADED_HASH = exports.UPGRADED_FUNCTION = exports.TYPE = exports.TIMESTAMP = exports.SHA512 = exports.SHA384 = exports.SHA256 = exports.SHA224 = exports.SHA1 = exports.NEW_CARD_HASH = exports.MD5 = exports.HASH_FUNCTION_ORDER = exports.HASH_ALGORITHM_HIERARCHY = exports.HASH = exports.FIRST_G_TIME = exports.EXISTING_CARD_HASH = exports.DUPLICATE_TIME = exports.DUPLICATE_EVENT_TYPE = exports.CONTENT_TYPE = exports.CONTENT_SIZE = exports.COLLISION_TIME = exports.COLLISION_EVENT_TYPE = void 0;
exports.generateCollisionEvent = generateCollisionEvent;
exports.generateDuplicationEvent = generateDuplicationEvent;
exports.nextHashFunction = nextHashFunction;
var _enums = require("./hash/enums.js");
var _validator = _interopRequireDefault(require("./hash/validator.js"));
var _g_time = require("./g_time.js");
var _config_constants = require("../config/config_constants.js");
// Use global Buffer if available, otherwise use the polyfill
const Buffer = global.Buffer || require('buffer').Buffer;
// Destructure the enum values
const {
MD5,
SHA1,
SHA224,
SHA256,
SHA384,
SHA512,
DEFAULT
} = _enums.HashAlgorithm;
// Event type constants
exports.SHA512 = SHA512;
exports.SHA384 = SHA384;
exports.SHA256 = SHA256;
exports.SHA224 = SHA224;
exports.SHA1 = SHA1;
exports.MD5 = MD5;
const TYPE = exports.TYPE = 'type';
const HASH = exports.HASH = 'hash';
const CONTENT_TYPE = exports.CONTENT_TYPE = 'content_type';
const TIMESTAMP = exports.TIMESTAMP = 'timestamp';
const EXISTING_CARD_HASH = exports.EXISTING_CARD_HASH = 'existing_card_hash';
const NEW_CARD_HASH = exports.NEW_CARD_HASH = 'new_card_hash';
const FIRST_G_TIME = exports.FIRST_G_TIME = 'first_g_time';
const CONTENT_SIZE = exports.CONTENT_SIZE = 'content_size';
const COLLISION_TIME = exports.COLLISION_TIME = 'collision_time';
const UPGRADED_FUNCTION = exports.UPGRADED_FUNCTION = 'upgraded_function';
const UPGRADED_HASH = exports.UPGRADED_HASH = 'upgraded_hash';
const DUPLICATE_TIME = exports.DUPLICATE_TIME = 'duplicate_time';
const DUPLICATE_EVENT_TYPE = exports.DUPLICATE_EVENT_TYPE = 'duplicate';
const COLLISION_EVENT_TYPE = exports.COLLISION_EVENT_TYPE = 'collision';
// Predefined hash function progression order
const HASH_FUNCTION_ORDER = exports.HASH_FUNCTION_ORDER = ['md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'];
// Create a new HashValidator instance for use
let hashValidator;
// Initialize hashValidator in a try-catch to handle potential initialization errors
try {
hashValidator = new _validator.default(Buffer.from(''), 'sha256');
} catch (error) {
console.warn('Failed to initialize hashValidator:', error);
// Create a mock hashValidator if initialization fails
hashValidator = {
validate: () => Promise.resolve(true),
computeHash: () => 'mocked-hash'
};
}
// Explicitly define HASH_ALGORITHM_HIERARCHY
const HASH_ALGORITHM_HIERARCHY = exports.HASH_ALGORITHM_HIERARCHY = {
[MD5]: [SHA1],
[SHA1]: [SHA224],
[SHA224]: [SHA256],
[SHA256]: [SHA384],
[SHA384]: [SHA512],
[SHA512]: []
};
// Replace existing nextHashFunction with method from instance
function nextHashFunction(currentHashFunction) {
const algMap = {
'md5': MD5,
'sha1': SHA1,
'sha224': SHA224,
'sha256': SHA256,
'sha384': SHA384,
'sha512': SHA512
};
const currFunc = algMap[currentHashFunction] || currentHashFunction;
const hashFunctions = Object.values(_enums.HashAlgorithm).filter(func => func !== currFunc && typeof func === 'string');
const strongerFunctions = hashFunctions.filter(func => {
const currentStrongerFuncs = HASH_ALGORITHM_HIERARCHY[currFunc] || [];
return currentStrongerFuncs.includes(func);
});
return strongerFunctions.length > 0 ? strongerFunctions[0] : currFunc;
}
// Generate a duplication event for the given card
// @param {Object} card - The card being duplicated
// @returns {string} JSON-stringified duplication event
function generateDuplicationEvent(card) {
const event = {
[TYPE]: DUPLICATE_EVENT_TYPE,
[HASH]: card.hash,
[CONTENT_TYPE]: card.contentType,
[TIMESTAMP]: _g_time.GTime.stampNow(card.hashFunction || _enums.HashAlgorithm.DEFAULT),
[DUPLICATE_TIME]: card.g_time
};
return JSON.stringify(event);
}
// Generate a collision event for the given event data
// @param {Object} newCard - The new card
// @param {Object} existingCard - The existing card
// @returns {string} JSON-stringified collision event
function generateCollisionEvent(newCard, existingCard = null) {
const event = {
type: 'collision',
original_hash: existingCard ? existingCard.hash : null,
new_hash: newCard.hash,
timestamp: _g_time.GTime.stampNow(newCard.hashFunction || existingCard?.hashFunction || _enums.HashAlgorithm.DEFAULT),
content_size: typeof newCard.content === 'string' ? Buffer.from(newCard.content).length : newCard.content.length
};
if (existingCard) {
const upgradedFunction = nextHashFunction(existingCard.hashFunction);
event.upgraded_function = upgradedFunction;
event.upgraded_hash = existingCard.hash;
event.hash_algorithm = upgradedFunction;
}
return JSON.stringify(event);
}
//# sourceMappingURL=event-producer.js.map