惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog
The Cloudflare Blog
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
T
Tailwind CSS Blog
L
LangChain Blog
Recent Announcements
Recent Announcements
Hugging Face - Blog
Hugging Face - Blog
Microsoft Security Blog
Microsoft Security Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
V
V2EX
I
InfoQ
博客园 - 司徒正美
T
The Blog of Author Tim Ferriss
G
Google Developers Blog
云风的 BLOG
云风的 BLOG
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
H
Help Net Security
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
B
Blog RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Debugging a Cross-Language HMAC Signature Failure Between...
Rahim Ranxx · 2026-05-16 · via DEV Community

Introduction

A few days ago, I hit a frustrating issue while integrating a custom Nextcloud application with a Django REST Framework backend.

Everything looked correct:

  • shared HMAC secret ✔️
  • canonical request string ✔️
  • HMAC-SHA256 ✔️
  • timestamps synchronized ✔️

Yet every authenticated request failed with:

invalid nextcloud signature

Enter fullscreen mode Exit fullscreen mode

The interesting part?

Both implementations were technically correct.

The failure came from something much smaller — and much more dangerous in distributed systems:

Different string encodings of the exact same HMAC digest.

This article walks through the full debugging process, the root cause, and the engineering lessons learned from debugging cryptographic interoperability between PHP and Python services.


System Architecture

The integration architecture looked like this:

┌──────────────────────┐
│  Nextcloud App (PHP) │
│  Generates HMAC      │
└──────────┬───────────┘
           │
           │ Signed HTTP Request
           ▼
┌──────────────────────┐
│ Django DRF Backend   │
│ Verifies Signature   │
└──────────────────────┘

Enter fullscreen mode Exit fullscreen mode

The request flow:

  1. Nextcloud generates a canonical request string
  2. PHP computes an HMAC-SHA256 signature
  3. Signature is attached to request headers
  4. Django reconstructs the canonical string
  5. Django recomputes the HMAC
  6. Signatures are compared

Simple in theory.

Except it kept failing.


Initial Symptoms

The backend logs showed repeated authorization failures:

nextcloud_hmac.denied
code=invalid_signature

Enter fullscreen mode Exit fullscreen mode

Even more confusing:

  • the integration had worked before
  • secrets matched
  • clocks matched
  • payloads matched

At first glance, it looked like a replay issue, timestamp skew problem, or cache corruption.

It turned out to be none of those.


The Root Cause

The issue came from a mismatch in how the HMAC digest was encoded.

Nextcloud (PHP)

The PHP client generated the signature like this:

base64_encode(
    hash_hmac('sha256', $canonical, $secret, true)
);

Enter fullscreen mode Exit fullscreen mode

Notice the important detail:

true

Enter fullscreen mode Exit fullscreen mode

That parameter returns the raw digest bytes.

Those bytes were then encoded as Base64.


Django (Python)

Meanwhile, Django verified signatures like this:

hmac.new(
    secret,
    canonical.encode(),
    hashlib.sha256,
).hexdigest()

Enter fullscreen mode Exit fullscreen mode

hexdigest() returns a hexadecimal string representation.

So both systems produced:

  • the same HMAC bytes
  • using the same algorithm
  • using the same secret

But converted those bytes into different string formats.


The Hidden Interoperability Bug

This was the breakthrough moment.

The exact same digest bytes produced:

Hex:
44c39c4ecc7268547ca51db72c6f27125251e6ea8ce3c659d918a9542522b612

Enter fullscreen mode Exit fullscreen mode

vs

Base64:
RMOcTsxyaFR8pR23LG8nElJR5uqM48ZZ2RipVCUithI=

Enter fullscreen mode Exit fullscreen mode

Both values represent the same underlying bytes.

But string comparison obviously fails.


The Second Bug

While investigating, I found another subtle issue.

The Django verifier lowercased the incoming signature before comparison:

signature = signature.lower()

Enter fullscreen mode Exit fullscreen mode

That may appear harmless for hexadecimal values.

But Base64 is case-sensitive.

Meaning:

ABC != abc

Enter fullscreen mode Exit fullscreen mode

So even after fixing the encoding mismatch, lowercasing would still break verification.

This was a protocol normalization bug hiding inside the verification pipeline.


The Fix

I updated Django to verify signatures using Base64 instead of hexadecimal.

New Verification Function

import base64
import hashlib
import hmac


def compute_hmac_signature_b64(
    *,
    secret: bytes,
    canonical_string: str,
) -> str:
    """Compute Base64 encoded HMAC-SHA256 signature."""

    digest = hmac.new(
        secret,
        canonical_string.encode("utf-8"),
        hashlib.sha256,
    ).digest()

    return base64.b64encode(digest).decode()

Enter fullscreen mode Exit fullscreen mode

Then all verification calls were updated to use:

compute_hmac_signature_b64()

Enter fullscreen mode Exit fullscreen mode

instead of:

.hexdigest()

Enter fullscreen mode Exit fullscreen mode

Finally, I removed:

.lower()

Enter fullscreen mode Exit fullscreen mode

from the verification flow.


Verification Results

After deploying the fix:

Ping Endpoint

GET /api/v1/integrations/nextcloud/ping/

200 OK

Enter fullscreen mode Exit fullscreen mode

Token Issuance

POST /api/v1/integrations/token/

200 OK

Enter fullscreen mode Exit fullscreen mode

Authentication immediately started working again.


Secondary Investigation Findings

While debugging, I validated several other production concerns.

1. Time Drift

I suspected clock skew initially.

Both services were checked:

Nextcloud epoch: 1778841776
Django epoch:    1778841776
Drift:            0 seconds

Enter fullscreen mode Exit fullscreen mode

Time synchronization was perfect.


2. Shared Secrets

Client IDs and secrets matched correctly across both systems.

This eliminated:

  • environment mismatch
  • stale secrets
  • config drift

3. Redis and Cache State

I flushed:

  • Redis
  • Django cache
  • integration token caches

This helped eliminate stale token artifacts and replay-state inconsistencies.


4. Infrastructure Validation

I also verified:

  • loopback networking
  • gunicorn binding
  • uvicorn workers
  • allowlists
  • HTTP dev mode configuration

At this point the investigation became less about cryptography and more about systematic elimination of variables.


Why It “Worked Before”

This was the most interesting systems question.

I had not changed the signing logic recently.

So why did the failure suddenly appear?

The likely answer is:

Infrastructure state had been masking a latent protocol incompatibility.

Possible contributors:

  • cached tokens
  • stale replay windows
  • inactive code paths
  • existing sessions bypassing verification
  • Redis persistence behavior

This is an important engineering lesson:

A system can contain dormant interoperability bugs for weeks before infrastructure conditions expose them.


Engineering Lessons Learned

1. Cryptographic Bytes ≠ String Representation

HMAC output is binary data.

Hexadecimal and Base64 are merely different textual encodings of the same bytes.

They are not interchangeable.


2. Cross-Language Integrations Need Explicit Contracts

Never assume:

  • encoding format
  • canonicalization rules
  • normalization behavior

Define them explicitly.

Especially across:

  • PHP
  • Python
  • Go
  • Node.js
  • Java

3. Normalization Can Break Security

Lowercasing signatures looked harmless.

It was not.

Cryptographic values should only be normalized if the protocol explicitly defines normalization behavior.


4. Infrastructure State Can Hide Bugs

Cache layers and token persistence can temporarily conceal protocol inconsistencies.

Sometimes:

  • restarts
  • cache flushes
  • clock resets

suddenly expose issues that already existed.


5. Production Debugging Requires Elimination Discipline

The investigation involved validating:

  • clocks
  • secrets
  • caches
  • workers
  • networking
  • encoding
  • replay protection
  • request canonicalization

Good debugging is often less about guessing and more about systematically removing uncertainty.


Final Thoughts

The most dangerous bugs are not always algorithm failures.

Sometimes:

  • the crypto is correct
  • the infrastructure is healthy
  • the logic is valid

…but the protocol contract between systems is inconsistent.

In this case:

The cryptography was correct on both sides. The protocol contract was not.

And that single mismatch was enough to break the entire authentication flow.