Skip to content

Instantly share code, notes, and snippets.

@atoponce
Last active July 11, 2026 23:31
Show Gist options
  • Select an option

  • Save atoponce/aab1532438dd047d73567edccc3ecf93 to your computer and use it in GitHub Desktop.

Select an option

Save atoponce/aab1532438dd047d73567edccc3ecf93 to your computer and use it in GitHub Desktop.
Mouse Entropy Implementations

My Approach

https://github.com/atoponce/scripts/blob/master/mouse-entropy.html

  • Generate a 512×512 pixel random bitimage with the browser CSPNG and animate it.
  • Record the pixel color (bit) underneath the (x, y) coordinate at event interrupt.
  • Collect 8 bits, then:
    • Collect and store the timestamp least significant byte into the entropy pool.
    • Assemble coordinate bits as single byte and store in the entropy pool.
    • Store the vector distance from the canvas origin (0, 0).
    • Store the vector magnitude (angle in arcseconds) from the canvas origin (0, 0).
  • After 32 events (128 total collected bytes), hash with SHA-256.
  • Stop the animation, release the event handler, and clear the entropy pool.
  • Bits are only recorded if the animation frame ID modulo 3 is 0, to reduce correlated mouse events.

KeePass

https://github.com/dlech/KeePass2.x/blob/official/KeePass/Forms/EntropyForm.cs

  • Generate a random grayscale bitmap grid
    • For visual reference only. The pixels in the bitmap are not used in the entropy generation.
  • Store an 8-byte timestap, 4-byte x-coordinate, and 4-byte y-coordinate at event interrupt in the entropy pool.
  • Hash the entropy pool with SHA-256.
  • Award 0.125 bits per collection.
  • Stop collecting after 256 bits have been awarded.

PuTTY

https://git.tartarus.org/simon/putty.git

  • Rate limit collection to 10 ms (previously 5ms) to prevent high-polling-rate devices (e.g., gaming mice sending 1,000 events/second) from flooding the buffer with redundant data.
  • 2 bits of entropy are assigned per sample.
  • 128 samples are collected for 256 bits entropy.
    • Timestamp is stored in the entropy pool.
    • (x, y) coordinates are combined with (x << 16) | y.
    • Coordinates stored in the next entropy pool index.
  • Entropy pool seeds the Fortuna RNG.

VeraCrypt

https://github.com/veracrypt/VeraCrypt/blob/master/src/Common/Random.c

  • Collect mouse event data (all mouse hook info, not just (x, y) coords).
  • Collect system uptime in ms.
  • Get time detla between current time and last collection time.
  • Calculate the CRC-32 of mouse event data:
    • Byte-by-byte in the mouse struct.
    • Store in crcCoords.
  • Discard if the current CRC-32 matches the last mouse CRC-32.
  • Calculate the CRC-32 of the time delta:
    • Byte-by-byte in the delta.
    • Store in crcTime.
  • Calculate the CRC-32 of the current time:
    • Byte-by-byte in the timestamp.
    • Store in crcTime.
  • Combine crcCoords and crcTime
  • Add combinded CRC-32 hashes to the entropy pool
    • Split the 32-bit integer into 4 bytes
    • Add to the rotating pool
    • Mix each byte into the entropy pool at the current index mod 256
    • Increment the current index
  • Update last CRC checks

About VeraCrypt's rotating entropy pool:

  • The pool is exactly 320 bytes in size
  • The pool operates as a ring buffer:
    • A cursor (index) tracks the current write position.
    • As new data arrives, the data is mixed, and the cursor is incremented, wrapping around as necessary.
  • Incoming data is split into individual bytes
  • Each byte is added to the current pool index mod 256
  • After 16 bytes are written:
    • The pool is hashed with user-selected algorithm (e.g., SHA-256)
    • The hash is split into individual bytes and mixed back into the pool blocks.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment