5 ms·
Why don't README's state how the main thing is done? CREATE EXTENSION IF NOT EXISTS "pgcrypto"; LOOP ... -- 8 bytes gives a collision p = .5 aft
by karmakaze 6y ago
Why don't README's state how the main thing is done?
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
LOOP
...
-- 8 bytes gives a collision p = .5 after 5.1 x 10^9 values
gkey := encode(gen_random_bytes(8), 'base64');
gkey := replace(gkey, '/', '_'); -- url safe replacement
gkey := replace(gkey, '+', '-'); -- url safe replacement
key := rtrim(gkey, '='); -- cut off padding
...
END LOOP
- tomcam 6y agoWell you just save me a ton of time. Thank you
- ben509 6y agoGenerally so you don't have to update the README every time you update the source.
- karmakaze 6y agoI wasn't suggesting that the actual code be in the README--that was laziness on my part--more in terms of what's used and expected output characteristics. I would specifically want to know if I was using a library with a certain implementation, changed how it did its main thing in a potentially meaningful way (e.g. dependencies, formulas, character set, output length, etc). A README is a good place to document these points.
- ben509 6y agoFair enough. If it's bugged you enough to think through what you're looking for in a README, that'd be a good post for HN.
- contravariant 6y agoWeird choice to pick 8 bytes then cut off the padding, you're effectively not using 2 bits in your last character that way. Might as well make it 12 characters instead of 11 and encode a full 9 bytes. Although seems like Youtube's done the same thing.
- irq-1 6y agothe '=' isn't url safe.
- nostoc 6y agoThat's not the point. encoding 9 bytes doesn't give any '=', because 3 bytes encode to 4 b64 characters.
- ignoramous 6y agoAFAIK: '=' is URI (path) safe [0] '=' is not query-param safe [1] Ex: [0] https://golang.org/pkg/net/url/#PathEscape https://golang.org/pkg/net/url/#PathEscape [1] https://golang.org/pkg/net/url/#QueryEscape https://golang.org/pkg/net/url/#QueryEscape
- gregmac 6y agoWell you do save a byte in the actual key. I guess if you really want to keep it to 11 bytes, it would be better to generate 9 bytes of random data, base64 it (which uses the full 12 characters, with no padding), and truncate to 11. You're throwing away 6 bits of random, but now all 88 bits in your 11-character key are useful (instead of only 86 bits).