3 ms·
Wait. Stop. You are writing your own crypto. AES keys are 256 (or 128) bit, fixed size, and should be random-looking data. The code here uses [32]byte intentio
by FiloSottile 10y ago
Wait. Stop. You are writing your own crypto.
AES keys are 256 (or 128) bit, fixed size, and should be random-looking data. The code here uses [32]byte intentionally so you won't use anything else.
If you have a password instead, that is NOT suitable for use as a key directly, whichever the length, because it has low entropy by definition. So you want to stretch it (and whiten it) with something like scrypt. It's a bit of the same argument as password hashing.
Also, you don't want to discover what happens if you have strong patterns like padding in your key (it should be fine, but it's the kind of attacks that come before a full break). And you don't want to cut short a password longer than 32 characters.
This is the issue, btw: https://github.com/gtank/cryptopasta/issues/7 https://github.com/gtank/cryptopasta/issues/7
Don't copy-paste crypto from HN.
- dawkins 10y agoI know! That's why I am asking. I have found surprisingly difficult to find a package that does this.
- elithrar 10y ago> I know! That's why I am asking. I have found surprisingly difficult to find a package that does this. scrypt, with an output length that suits your use-case? (as Filippo points out) Note: I wrote https://github.com/elithrar/simple-scrypt https://github.com/elithrar/simple-scrypt, which wraps Go's scrypt package and gives it a friendlier API (mimicking the bcrypt one). Handles salt generation for you and has sane default parameters, outputting a 32-byte derived key by default. e.g. func deriveKey(key []byte) ([]byte, error) { return scrypt.GenerateFromPassword([]byte("plaintext"), scrypt.DefaultParams) } You can pass your own params, or modify the defaults to get a different key length depending on your use-case.
- dawkins 10y agoyes, that is what I was looking for. Thank you!