compareHashes
Compare two cryptographic hashes in constant time to verify data integrity and detect tampering without 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 handler function that implements the core logic of the 'compareHashes' tool, converting hashes to buffers, checking lengths, and performing constant-time comparison using Buffer.compare 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)The input schema defining the required 'hash1' and 'hash2' string parameters for the 'compareHashes' tool.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)The complete tool definition object for 'compareHashes' exported as part of securityTools, which includes name, description, inputSchema, and handler.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'}`); } } }
- src/index.ts:28-35 (registration)Registration of the 'compareHashes' tool (via securityTools) into the central allTools object used by the MCP server for tool listing and execution.const allTools: ToolKit = { ...systemTools, ...networkTools, ...geoTools, ...generatorTools, ...dateTimeTools, ...securityTools };