How to Mock Image Signing Cost in Python

Create a deterministic mock signing cost calculator that predicts resource usage for image signatures before real signing infrastructure is staged.

Medium Python 3.9+ Aug 9, 2026 Production deployment patterns 14 views 0 copies

Python code

24 lines
Python 3.9+
import math
import struct


def sign_image_cost(image_signature: bytes) -> int:
    """Deterministic mock signing cost based on image signature bytes."""
    if not image_signature:
        raise ValueError("Empty image signature")
    digest = 0
    for byte in image_signature:
        digest = (digest * 31 + byte) & 0xFFFFFFFF
    # Costs depend on image size (first 4 bytes as little-endian) and complexity
    size = struct.unpack("<I", image_signature[:4])[0]
    complexity = digest % 100
    return 10 + size // 1024 + complexity


if __name__ == "__main__":
    # Simulating an image signature: [size_bytes_le][sha256_partial]
    size_bytes = struct.pack("<I", 2048)
    hash_part = bytes(range(32))
    sig = size_bytes + hash_part
    cost = sign_image_cost(sig)
    print(f"Mock signing cost for image: {cost} units")

Output

stdout
Mock signing cost for image: 44 units

How it works

The sign_image_cost function computes a hash-based digest using a 31-multiplier rolling hash to produce a deterministic complexity score. The image size is extracted from the first four bytes as little-endian using struct.unpack and contributes proportional cost. Complexity percentage (digest mod 100) adds variability while remaining predictable for testing. The mock enables pipeline testing without invoking real signing services, which is critical before production rollout.

Common mistakes

  • Forgetting to sanity-check `image_signature[:4]` before unpacking, causing struct errors
  • Assuming costs are actual wall-clock timings rather than abstract units
  • Ignoring hash collision effects on complexity distribution

Variations

  1. Use hashlib.sha256 for a more realistic digest in the signature
  2. Replace struct parsing with int.from_bytes(image_signature[:4], 'little')

Real-world use cases

  • Estimating signing cost during blue-green deployment rehearsals before switching production traffic
  • Providing cost-aware autoscaling inputs for a signing worker pool in Kubernetes deployments
  • Testing canary rollout hooks that budget signing overhead before full fleet rollout

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Production deployment patterns

Related tutorials and quizzes for this topic.