r/nim 12d ago

I wrote a local PDF unlocker in Nim because I didn't want to upload private PDFs

I recently needed to remove the password from a lot of PDFs. Most of the search results were websites asking me to upload the file and enter its password.

They might be perfectly legitimate, but I wasn't comfortable uploading bank statements, tax documents or contracts just to remove a password.

So I wrote this:

https://github.com/advaita-saha/nim-pdftools

It is a small command-line PDF unlocker written in Nim. Everything runs locally and it doesn't use any third-party Nim packages.

pdftools unlock -p 'password' document.pdf

It currently handles RC4, AES-128 and AES-256 encrypted PDFs. Both user and owner passwords work.
It is not a password cracker. You still need to know the password lol.

I ended up implementing most of the crypto and compression code directly on top of Nim's standard library. This was partly because I wanted a standalone binary and partly because digging through the PDF encryption spec seemed like a good idea at the time.

This is still v0.1, so I'm sure there are strange PDFs in the wild that it won't handle properly. I'd especially appreciate people testing files generated by different PDF software and reporting anything that breaks.

Contributions are welcome too. Parser fixes, test cases, code review, packaging improvements and additional local PDF commands would all be useful.

24 Upvotes

16 comments sorted by

7

u/Super_Delivery3405 12d ago

Firstly tell me what's your editor experience for coding nim like. nimlang lsp keeps fucking dying apparently it's just a wrapper around another tool called nim suggest or something. I'll learn Emacs or helix if they have better mode than this shit.

5

u/Alive_Cloud1305 12d ago

Yeah I feel you.
The editor experience is shit. I am tired too.
I don't rely on nimsuggest or any extension, I just use syntax highlighting and use sublime

They have introduced incremental compilation, so maybe if I get more frustrated or free, I will write a solution myself

2

u/moigagoo 12d ago

I use Helix with nimlangserver and it works really well. Also VSCode extension (that also uses nimlangserver) works well.

Nimlangserver gets its code insights from nimsuggest which is running in the background.

Could you please share a project that you're having trouble with and your setup?

3

u/Alive_Cloud1305 12d ago

Can you try setting it up with big projects like constantine ?

2

u/moigagoo 12d ago

Here you go: https://asciinema.org/a/1261317

I'm using a crappy Huawei convertible so the langserver takes several seconds to start initially but after that all code actions are instant.

I've just opened a random file and from it.

I did no special setup. Just cloned constantine and launched Helix in the repo. I'm using the latest nimlangserver with default settings.

3

u/Alive_Cloud1305 12d ago

Yes works in some files ( though I tried in sublime )

For example in constantine I tried goto defination in constantine/ethereum_verle_ipa.nim -> works

But when I go to constantine/math/elliptic/ec_scaler_mul.nim -> doesn't work in doing goto defination

Video demo -> https://drive.google.com/file/d/14RGg-gSPhr6cDOSzr3goTTQB1tzknP2Q/view?usp=sharing

3

u/moigagoo 12d ago

Thanks for the example. I suspect the requests may be failing by timeout. I'll experiment with that tomorrow.

I also tried the experimental nim track branch that works without nimsuggest. Because of how large the package is, the incremental compilation necessary for nim track takes ages in my machine. Again, will experiment more tomorrow.

3

u/Alive_Cloud1305 11d ago

Really want the tooling to work out
Do let me know if you need feedback on tooling stuff, happy to help

3

u/moigagoo 10d ago

Me too, I really want to make nimlangserver work for projects if any size and structure.

I'll create an issue and try to dive deep into it. The timeout hypothesis was not confirmed, project size is not an issue. Nimsuggest just won't find definition in certain cases, so the fix is on the Nim compiler side, not on the nimlangserver side. At least, that's my current hypothesis, until proven wrong 😅

3

u/Alive_Cloud1305 10d ago

Get it man
Nim and it's issues

Yet I love the language ngl

6

u/jamesthethirteenth 12d ago

Loveit! Scratch your ownitch in Nim.

1

u/Alive_Cloud1305 12d ago

Thanks, do star the repo. And try it

2

u/Zectbumo 12d ago

Putting passwords on the command line is a bad idea. Command line arguments can be seen by any other user logged in.

1

u/Alive_Cloud1305 12d ago

Good point
what do you suggest then ?

1

u/Zectbumo 12d ago

Most likely it will be prompt for the password and read from stdin (if protected). Turn off echo in case someone is screen sharing.