hash

Contrary to Python 3.1 Docs, hash(obj) != id(obj). So which is correct?

The following is from the Python v3.1.2 documentation: From The Python Language Reference Section 3.3.1 Basic Customization: object.__hash__(self) ... User-defined classes have __eq__() and __hash__() methods by default; with them, all objects compare unequal (except with themselves) and x.__hash__() returns id(x). From The Glossar...

Is it possible to use a Perl hash in a manner that has `O(log(n))` lookup and insertion?

Is it possible to use a Perl hash in a manner that has O(log(n)) lookup and insertion? By default, I assume the lookup is O(n) since it's represented by an unsorted list. I know I could create a data structure to satisfy this (ie, a tree, etc) however, it would be nicer if it was built in and could be used as a normal hash (ie, with %...

Convert URI to GUID

What is a good way to convert a file path (URI) into a System.Guid? I'd like to minimize the possibility of a collision, but I'm happy with a reasonably unique hashing (probably never more than a few dozen/hundred items in the database) ...

Arrays/Lists and computing hashvalues (VB, C#)

I feel bad asking this question but I am currently not able to program and test this as I'm writing this on my cell-phone and not on my dev machine :P (Easy rep points if someone answers! XD ) Anyway, I've had experience with using hashvalues from String objects. E.g., if I have StringA and StringB both equal to "foo", they'll both com...

What hash function yields the shortest output?

What is the shortest hash that can be obtained and why? What happens if you try to achieve a shorter one, is there a fixed limit or does it depend on the length of the hashed data and why? ...

Flattening hash into string in Ruby

Is there a way to flatten a hash into a string, with optional delimiters between keys and values, and key/value pairs? For example, print {:a => :b, :c => :d}.flatten('=','&') should print a=b&c=d I wrote some code to do this, but I was wondering if there was a neater way: class Hash def flatten(keyvaldelimiter, entrydelimiter) ...

Perl, get all hash values

Let's say in Perl I have a list of hash references, and each is required to contain a certain field, let's say foo. I want to create a list that contains all the mappings of foo. If there is a hash that does not contain foo the process should fail. @hash_list = ( {foo=>1}, {foo=>2} ); my @list = (); foreach my $item (@hash_list) { ...

How to properly define hash function for a list of objects?

I have a data structure containing a list of objects, like this: class A { private List<Object> list; } How to properly define a hash function for the list, assuming each element of the list has correct hashCode()? (It's strange that I couldn't easily find the solution via Google.) ...

Hashing Credentials in Objective C

I am trying to store a username and password to hash against for future offline logging in. What is the best way to do this in objective c? I will need the password to be stored securely. ...

Hash 32bit int to 16bit int?

What are some simple ways to hash a 32-bit integer (e.g. IP address, e.g. Unix time_t, etc.) down to a 16-bit integer? E.g. hash_32b_to_16b(0x12345678) might return 0xABCD. Let's start with this as a horrible but functional example solution: function hash_32b_to_16b(val32b) { return val32b % 0xffff; } Question is specifically ab...

How can I convert a C# list into something that's hashable?

I want something along the lines of Python's tuples (or, for sets, frozensets), which are hashable. I have a List<String> which is most certainly not hashing correctly (i.e. by value). ...

Do cryptographic hashes provide really unique results?

I was wondering whether md5, sha1 and anothers return unique values. For example, sha1() for test returns a94a8fe5ccb19ba61c4c0873d391e987982fbbd3, which is 40 characters long. So, sha1 for strings larger than 40 chars must be the same (of course it's scrambled, because the given input may contain whitespaces and special chars etc.). D...

How do I access the value for this nested hash's key in Ruby?

I have the following hash: {:charge_payable_response=>{:return=>"700", :ns2=>"http://ws.myws.com/"}} How can I get the value of the key :return, which in this example is 700? ...

how to build a index table(python dict like) in python with sqlite3

Suppose I have one string list may have duplicated items: A B C A A C D E F F I want to make a list can assign an unique index for each item, looks like: 1 A 2 B 3 C 4 D 5 E 6 F now I created sqlite3 database with below SQL statement: CREATE TABLE aa ( myid INTEGER PRIMARY KEY AUTOINCREMENT, name STRI...

What is a hash function in java?

I have check out this Wikipedia page on it, but I still don't understand it. Can someone please help my dim-witted mind to understand the concepts of hashing, hashtable/hashmap, and hash functions? Some examples would really help. ...

whats wrong with this ruby hash?

I'm pretty new to ruby, I keep getting the following error: in gem_original_require': ./helpers/navigation.rb:28: odd number list for Hash (SyntaxError) Any help appreciated... module Sinatra::Navigation def navigation @navigation nav = { primary[0] = { ...

Top Hashing and Encryption Algorithms?

I know many web projects still use the older MD5() or SHA1() when creating hashes. However, in my projects I have been using SHA256 for stronger/longer hashes since when I last checked the there was some question about which hashes were the bester ones to use. So I just chose the government standard (at the time). However, I'm wondering...

Python: Is this an ok way of overriding __eq__ and __hash__?

I'm new to Python, and I wanted to make sure that I overrode __eq__ and __hash__ correctly, so as not to cause painful errors later: (I'm using Google App Engine.) class Course(db.Model): dept_code = db.StringProperty() number = db.IntegerProperty() title = db.StringProperty() raw_pre_reqs = db.StringProperty(multiline=...

MessageDigest hashes differently on different machines

I'm having a problem with MessageDigest returning different hash values on different computers. One computer is running 32-bit Java on Windows Vista and the other is running 64-bit Java on Mac OS. I'm not sure if it is because MessageDigest is machine dependent, or I need to explicitly specify a character encoding somewhere, or perhap...

C#: optimizing dictionary access (hash in key structures)

So, I need to create a struct in C# that will act as a key into a (quite large) dictionary, will look like this: private readonly IDictionary<KeyStruct, string> m_Invitations; Problem is, I REALLY need a struct to use as a key, because it is only possible to identify entries via two separate data items, where one of them can be a null...