@onlineapps/service-validator-core 1.0.4 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onlineapps/service-validator-core",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "Core validation logic for microservices",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -7,6 +7,7 @@ const CertificateManager = require('./security/certificateManager');
7
7
  const ValidationProofVerifier = require('./security/ValidationProofVerifier');
8
8
  const ValidationProofCodec = require('./security/ValidationProofCodec');
9
9
  const ValidationProofSchema = require('./types/ValidationProofSchema');
10
+ const FingerprintUtils = require('./utils/FingerprintUtils');
10
11
 
11
12
  class ValidationCore {
12
13
  constructor(config = {}) {
@@ -213,4 +214,5 @@ class ValidationCore {
213
214
  module.exports = ValidationCore;
214
215
  module.exports.ValidationProofVerifier = ValidationProofVerifier;
215
216
  module.exports.ValidationProofCodec = ValidationProofCodec;
216
- module.exports.ValidationProofSchema = ValidationProofSchema;
217
+ module.exports.ValidationProofSchema = ValidationProofSchema;
218
+ module.exports.FingerprintUtils = FingerprintUtils;
@@ -1,4 +1,5 @@
1
1
  const crypto = require('crypto');
2
+ const FingerprintUtils = require('../utils/FingerprintUtils');
2
3
 
3
4
  class CertificateManager {
4
5
  constructor(config = {}) {
@@ -147,8 +148,7 @@ class CertificateManager {
147
148
  * @returns {string} Fingerprint
148
149
  */
149
150
  generateFingerprint(data) {
150
- const sortedData = JSON.stringify(data, Object.keys(data).sort());
151
- return crypto.createHash('sha256').update(sortedData).digest('hex');
151
+ return FingerprintUtils.generateValidationFingerprint(data);
152
152
  }
153
153
 
154
154
  /**
@@ -1,5 +1,6 @@
1
1
  const jwt = require('jsonwebtoken');
2
2
  const crypto = require('crypto');
3
+ const FingerprintUtils = require('../utils/FingerprintUtils');
3
4
 
4
5
  class TokenManager {
5
6
  constructor(config = {}) {
@@ -186,8 +187,7 @@ class TokenManager {
186
187
  * @returns {string} Fingerprint hash
187
188
  */
188
189
  generateFingerprint(validationData) {
189
- const sortedData = JSON.stringify(validationData, Object.keys(validationData).sort());
190
- return crypto.createHash('sha256').update(sortedData).digest('hex');
190
+ return FingerprintUtils.generateValidationFingerprint(validationData);
191
191
  }
192
192
  }
193
193
 
@@ -0,0 +1,126 @@
1
+ const crypto = require('crypto');
2
+
3
+ /**
4
+ * FingerprintUtils - Centralized fingerprint generation
5
+ *
6
+ * Single source of truth for SHA256 fingerprinting across the system.
7
+ * Used for configuration caching, certificate verification, and content integrity.
8
+ *
9
+ * NOT to be confused with ValidationProofCodec which is specifically for
10
+ * validation proof hashing (different purpose).
11
+ *
12
+ * Usage:
13
+ * - configFingerprint: Identify service configuration version
14
+ * - Certificate fingerprint: Verify certificate data integrity
15
+ * - Token fingerprint: Generate token identifiers
16
+ * - Content fingerprint: Verify file/storage content
17
+ */
18
+ class FingerprintUtils {
19
+ /**
20
+ * Generate SHA256 fingerprint for any data
21
+ *
22
+ * @param {*} data - Data to fingerprint (will be JSON.stringify if object)
23
+ * @param {Object} options - Options
24
+ * @param {boolean} options.sortKeys - Sort object keys (default: true)
25
+ * @returns {string} SHA256 hash (64 hex characters)
26
+ *
27
+ * @example
28
+ * const hash = FingerprintUtils.generate({ version: '1.0', endpoints: [] });
29
+ * // Returns: consistent SHA256 hash for the object
30
+ */
31
+ static generate(data, options = {}) {
32
+ const sortKeys = options.sortKeys !== false; // default true
33
+
34
+ let input;
35
+
36
+ // Handle different data types
37
+ if (Buffer.isBuffer(data)) {
38
+ input = data;
39
+ } else if (typeof data === 'string') {
40
+ input = data;
41
+ } else if (typeof data === 'object' && data !== null) {
42
+ // Serialize object with optional key sorting
43
+ const keys = sortKeys ? Object.keys(data).sort() : Object.keys(data);
44
+ input = JSON.stringify(data, keys);
45
+ } else {
46
+ // Primitives (number, boolean, null, undefined)
47
+ input = String(data);
48
+ }
49
+
50
+ // Generate SHA256 hash
51
+ return crypto.createHash('sha256').update(input).digest('hex');
52
+ }
53
+
54
+ /**
55
+ * Generate fingerprint for service configuration
56
+ * Used for cache validation - skip re-validation if config unchanged
57
+ *
58
+ * @param {Object} config - Service configuration
59
+ * @param {string} config.version - Service version (REQUIRED)
60
+ * @param {Array} config.endpoints - API endpoints (REQUIRED)
61
+ * @param {Object} config.metadata - Service metadata (REQUIRED)
62
+ * @param {string} config.health - Health check endpoint (REQUIRED)
63
+ * @returns {string} Configuration fingerprint
64
+ * @throws {Error} If any required field is missing
65
+ */
66
+ static generateConfigFingerprint(config) {
67
+ if (!config) {
68
+ throw new Error('Config is required for generateConfigFingerprint');
69
+ }
70
+ if (!config.version) {
71
+ throw new Error('config.version is required');
72
+ }
73
+ if (!Array.isArray(config.endpoints)) {
74
+ throw new Error('config.endpoints must be an array');
75
+ }
76
+ if (typeof config.metadata !== 'object' || config.metadata === null) {
77
+ throw new Error('config.metadata must be an object');
78
+ }
79
+ if (!config.health) {
80
+ throw new Error('config.health is required');
81
+ }
82
+
83
+ return this.generate({
84
+ version: config.version,
85
+ endpoints: config.endpoints,
86
+ metadata: config.metadata,
87
+ health: config.health
88
+ });
89
+ }
90
+
91
+ /**
92
+ * Generate fingerprint for validation results
93
+ * Used in certificates and tokens
94
+ *
95
+ * @param {Object} validationResults - Validation results data
96
+ * @returns {string} Validation results fingerprint
97
+ */
98
+ static generateValidationFingerprint(validationResults) {
99
+ return this.generate(validationResults);
100
+ }
101
+
102
+ /**
103
+ * Generate fingerprint for content (files, storage)
104
+ *
105
+ * @param {string|Buffer} content - Content to fingerprint
106
+ * @returns {string} Content fingerprint
107
+ */
108
+ static generateContentFingerprint(content) {
109
+ return this.generate(content, { sortKeys: false });
110
+ }
111
+
112
+ /**
113
+ * Verify fingerprint matches data
114
+ *
115
+ * @param {*} data - Data to verify
116
+ * @param {string} expectedFingerprint - Expected fingerprint
117
+ * @param {Object} options - Options (passed to generate)
118
+ * @returns {boolean} True if fingerprint matches
119
+ */
120
+ static verify(data, expectedFingerprint, options = {}) {
121
+ const actualFingerprint = this.generate(data, options);
122
+ return actualFingerprint === expectedFingerprint;
123
+ }
124
+ }
125
+
126
+ module.exports = FingerprintUtils;