compareHashes
Compare two cryptographic hashes securely in constant time to verify integrity and prevent timing attacks.
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)The async handler function that performs constant-time hash comparison using Buffer.compare, with length check to prevent timing attacks.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 requiring two string hashes: 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/tools/security.ts:61-114 (registration)Full tool definition object exported as part of securityTools, which is later included in the main allTools registry.compareHashes: { name: 'compareHashes', description: 'Compare two hashes in constant time', inputSchema: { type: 'object', properties: { hash1: { type: 'string', description: 'First hash to compare' }, hash2: { type: 'string', description: 'Second hash to compare' } }, required: ['hash1', 'hash2'] }, 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'}`); } } }