I wanted to download Mega links on a Mac without installing a Java runtime, so I wrote MegaGet: a small Node server plus a local web UI. Here is what turned out to be interesting.
1. Keys and AES-CTR
A Mega file key is 32 bytes. The AES key is the first half XOR the second half's first 16 bytes, the next 8 bytes are the CTR nonce, and the last 8 are the expected MAC. Because it is plain CTR, you can resume at any 16-byte-aligned offset by setting the counter to offset / 16 and requesting url/<offset>. That gives you resume after a dropped connection for free.
2. Verifying the file with aes-128-cbc
Mega's integrity check is a chunked CBC-MAC (chunks of 128 KB growing to 1 MB). It maps neatly onto Node's aes-128-cbc with no padding: encrypt the chunk with the nonce as IV and keep the last block as the chunk MAC. Fold the chunk MACs the same way, then XOR the halves to get the 8-byte tag. No per-block loop needed, so it is fast.
3. The 402 and the hashcash challenge
Logging in returned HTTP 402 with an X-Hashcash header. The client has to find a 4-byte nonce so that SHA-256 of nonce + token x 262144 falls under a threshold, then resend the same request (same id) with X-Hashcash: 1:<token>:<nonce>. I run the search on four worker_threads; it takes a couple of seconds.
4. What it does not do
It does not bypass Mega's transfer quota. When the quota is hit it waits and resumes; with a Mega account it uses that account's quota. It is download-only, and so far tested mostly against a mock Mega server, so real-world bug reports are very welcome.
Code (MIT): https://github.com/gianbenve95/megaget
I wrote it with Claude's help and reviewed and tested the result.
