compareHashes
Compare two hashes in constant time to ensure secure and consistent verification without timing vulnerabilities. Ideal for cryptographic and data integrity checks.
Instructions
Compare two hashes in constant time
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| hash1 | Yes | First hash to compare | |
| hash2 | Yes | Second hash to compare |
Implementation Reference
- src/tools/security.ts:78-113 (handler)Executes constant-time hash comparison using Buffer.from and Buffer.compare to prevent timing attacks, returning match result and lengths.handler: async ({ hash1, hash2 }: { hash1: string; hash2: string }) => { try { // Convert strings to buffers for constant-time comparison const buf1 = Buffer.from(hash1); const buf2 = Buffer.from(hash2); // Ensure same length to prevent timing attacks if (buf1.length !== buf2.length) { return { content: [{ type: 'text', text: JSON.stringify({ match: false, reason: 'Length mismatch' }, null, 2) }] }; } // Constant-time comparison const match = Buffer.compare(buf1, buf2) === 0; return { content: [{ type: 'text', text: JSON.stringify({ match, hash1Length: buf1.length, hash2Length: buf2.length }, null, 2) }] }; } catch (error) { throw new Error(`Hash comparison failed: ${error instanceof Error ? error.message : 'Unknown error'}`); } }
- src/tools/security.ts:64-77 (schema)Input schema defining two required string properties: hash1 and hash2.inputSchema: { type: 'object', properties: { hash1: { type: 'string', description: 'First hash to compare' }, hash2: { type: 'string', description: 'Second hash to compare' } }, required: ['hash1', 'hash2'] },
- src/index.ts:27-33 (registration)Registers compareHashes by spreading securityTools into allTools, used for tool listing (lines 112-116) and execution lookup (line 130).const allTools: ToolKit = { ...encodingTools, ...geoTools, ...generatorTools, ...dateTimeTools, ...securityTools };