ruby using the “&:methodname” shortcut from array.map(&:methodname) for hash key strings rather than methodname

You can do this with a lambda: extract_keyname = ->(h) { h[:keyname] } ary_of_hashes.map(&extract_keyname) This tends to be more useful if the block’s logic is more complicated than simply extracting a value from a Hash. Also, attaching names to your bits of logic can help clarify what a chain of Enumerable method calls is trying … Read more

How do methods use hash arguments in Ruby?

Example: def foo(regular, hash={}) puts “regular: #{regular}” puts “hash: #{hash}” puts “a: #{hash[:a]}” puts “b: #{hash[:b]}” end foo(“regular argument”, a: 12, :b => 13) I use hash={} to specify that the last argument is a hash, with default value of empty hash. Now, when I write: foo(“regular argument”, a: 12, :b => 13) It’s actually … Read more

Best practice for hashing passwords – SHA256 or SHA512?

Switching to SHA512 will hardly make your website more secure. You should not write your own password hashing function. Instead, use an existing implementation. SHA256 and SHA512 are message digests, they were never meant to be password-hashing (or key-derivation) functions. (Although a message digest could be used a building block for a KDF, such as … Read more

php: number only hash?

An MD5 or SHA1 hash in PHP returns a hexadecimal number, so all you need to do is convert bases. PHP has a function that can do this for you: $bignum = hexdec( md5(“test”) ); or $bignum = hexdec( sha1(“test”) ); PHP Manual for hexdec Since you want a limited size number, you could then … Read more

Why and how are Python functions hashable?

It’s nothing special. As you can see if you examine the unbound __hash__ method of the function type: >>> def f(): pass … >>> type(f).__hash__ <slot wrapper ‘__hash__’ of ‘object’ objects> the of ‘object’ objects part means it just inherits the default identity-based __hash__ from object. Function == and hash work by identity. The difference … Read more

How to send password securely via HTTP using Javascript in absence of HTTPS?

There is no way to send a password securely that the user can verify without SSL. Sure, you can write some JavaScript that will make a password secure for over-the-wire transmission through hashing or public-key-encryption. But how can the user be sure that the JavaScript itself has not been tampered with by a man-in-the-middle before … Read more