const { signRequest } = require('./app/lib/api_sign.js'); const query = { app_key: 'APP2024011514301234', method: 'openapi.index', sign: '806b7294f2545bf20eb1fdf244e7ff28', timestamp: 1777222912, v: '1.0', }; const body = {}; const self_app_secret = 'sec_abc123def456ghi789jkl012mno345pqr678stu901vwx234yz'; const providedSign = '806b7294f2545bf20eb1fdf244e7ff28'; try { const { canonicalQuery, bodySign, sign } = signRequest({ query, body, selfAppSecret: self_app_secret, }); console.log(`canonicalQuery: ${canonicalQuery}`); console.log(`bodySign: ${bodySign}`); console.log(`Calculated sign: ${sign}`); console.log(`Provided sign: ${providedSign}`); console.log(`Signs match: ${sign === providedSign}`); if (sign !== providedSign) { console.log('---'); console.log('Mismatch reason analysis:'); // 1. CanonicalQuery check const expectedCanonicalQuery = 'app_key=APP2024011514301234&method=openapi.index×tamp=1777222912&v=1.0'; if (canonicalQuery !== expectedCanonicalQuery) { console.log(`- CanonicalQuery calculation might be incorrect. Expected "${expectedCanonicalQuery}" but got "${canonicalQuery}". This could be due to wrong sorting order or incorrect handling of parameters.`); } // 2. BodySign check const crypto = require('crypto'); const expectedBodySign = crypto.createHash('md5').update('{}').digest('hex'); if (bodySign !== expectedBodySign) { console.log(`- BodySign calculation might be incorrect. For an empty object {}, expected "${expectedBodySign}" but got "${bodySign}". This could be due to differences in JSON serialization (e.g., whitespace).`); } // 3. Final hash check const signText = `${canonicalQuery}${self_app_secret}${bodySign}`; const expectedSign = crypto.createHash('md5').update(signText).digest('hex'); if (sign !== expectedSign) { console.log(`- The final hash calculation seems to be the issue. The string to be signed was "${canonicalQuery}${self_app_secret}${bodySign}". The resulting hash did not match the provided sign.`); } console.log('- The most likely reason is an error in the signing logic itself or a discrepancy in how the components (canonical query, body sign, secret) are combined before hashing.'); } } catch (error) { console.error('An error occurred during signature calculation:', error); }