hash
How to decrypt a SHA-256 encrypted string?
SHA-256 is a cryptographic (one-way) hash function, so there is no direct way to decode it. The entire purpose of a cryptographic hash function is that you can’t undo it. One thing you can do is a brute-force strategy, where you guess what was hashed, then hash it with the same function and see if … Read more
Types that define `__eq__` are unhashable?
Yes, if you define __eq__, the default __hash__ (namely, hashing the address of the object in memory) goes away. This is important because hashing needs to be consistent with equality: equal objects need to hash the same. The solution is simple: just define __hash__ along with defining __eq__.
Ruby: Creating a hash key and value from a variable in Ruby [duplicate]
If you want to populate a new hash with certain values, you can pass them to Hash::[]: Hash[“a”, 100, “b”, 200] #=> {“a”=>100, “b”=>200} Hash[ [ [“a”, 100], [“b”, 200] ] ] #=> {“a”=>100, “b”=>200} Hash[“a” => 100, “b” => 200] #=> {“a”=>100, “b”=>200} So in your case: Hash[id, ‘foo’] Hash[[[id, ‘foo’]]] Hash[id => ‘foo’] … Read more
What is the fastest hash algorithm to check if two files are equal?
Unless you’re using a really complicated and/or slow hash, loading the data from the disk is going to take much longer than computing the hash (unless you use RAM disks or top-end SSDs). So to compare two files, use this algorithm: Compare sizes Compare dates (be careful here: this can give you the wrong answer; … Read more
Why do salts make dictionary attacks ‘impossible’?
It doesn’t stop dictionary attacks. What it does is stop someone who manages to get a copy of your password file from using a rainbow table to figure out what the passwords are from the hashes. Eventually, it can be brute-forced, though. The answer to that part is to force your users to not use … Read more
In Ruby, how do I make a hash from an array?
Say you have a function with a funtastic name: “f” def f(fruit) fruit + “!” end arr = [“apples”, “bananas”, “coconuts”, “watermelons”] h = Hash[ *arr.collect { |v| [ v, f(v) ] }.flatten ] will give you: {“watermelons”=>”watermelons!”, “bananas”=>”bananas!”, “apples”=>”apples!”, “coconuts”=>”coconuts!”} Updated: As mentioned in the comments, Ruby 1.8.7 introduces a nicer syntax for this: … Read more
Should the hash code of null always be zero, in .NET
So long as the hash code returned for nulls is consistent for the type, you should be fine. The only requirement for a hash code is that two objects that are considered equal share the same hash code. Returning 0 or -1 for null, so long as you choose one and return it all the … Read more
How do I loop over a hash of hashes?
Value is a Hash to so you need iterate on it or you can get only values:- h.each do |key, value| puts key value.each do |k,v| puts k puts v end end or h.each do |key, value| puts key value.values.each do |v| puts v end end
PHP short hash like URL-shortening websites
TinyURL doesn’t hash anything, it uses Base 36 integers (or even base 62, using lower and uppercase letters) to indicate which record to visit. Base 36 to Integer: intval($str, 36); Integer to Base 36: base_convert($val, 10, 36); So then, instead of redirecting to a route like /url/1234 it becomes /url/ax instead. This gives you a … Read more