Skip to main content
Glama

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
NameRequiredDescriptionDefault
hash1YesFirst hash to compare
hash2YesSecond hash to compare

Implementation Reference

  • 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'}`); } }
  • 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'] },
  • 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'}`); } } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/cyanheads/toolkit-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server