hash

Convert an array into an index hash in Ruby

Let's say I have an array, and I want to make a hash so I can quickly ask "is X in the array?". In perl, there is an easy (and fast) way to do this: my @array = qw( 1 2 3 ); my %hash; @hash{@array} = undef; This generates a hash that looks like: { 1 => undef, 2 => undef, 3 => undef, } The best I've come up with in Ruby ...

Need some help understanding password salt

I'm having some trouble understanding the purpose of a salt to a password. It's my understanding that the primary use is to hamper a rainbow table attack. However, the methods I've seen to implement this don't seem to really make the problem harder. I've seen many tutorials suggesting that the salt be used as the following: $hash = ...

Good choice for a lightweight checksum algorithm?

Hi all, I find myself needing to generate a checksum for a string of data, for consistency purposes. The broad idea is that the client can regenerate the checksum based on the payload it recieves and thus detect any corruption that took place in transit. I am vaguely aware that there are all kinds of mathematical principles behind thi...

Ruby on Rails: hash.each {} issues

Here is my code: records_hash = records[:id].inject({}) { |result,h| if result.has_key?(h) result[h] += 1 else result[h] = 1 end result } @test2 = records_hash.each{|key,value| puts "#{key} is #{value}"} My output should look like this: bozo is 3 bubba is 4 bonker is 5 But it renders on the page (<%= @test2 %>) as ...

1-1 mappings for id obfuscation

I'm using sequential ids as primary keys and there are cases where I don't want those ids to be visible to users, for example I might want to avoid urls like ?invoice_id=1234 that allow users to guess how many invoices the system as a whole is issuing. I could add a database field with a GUID or something conjured up from hash function...

Techniques for implementing -hash on mutable Cocoa objects

The documentation for -hash says it must not change while a mutable object is stored in a collection, and similarly the documentation for -isEqual: says the -hash value must be the same for equal objects. Given this, does anybody have any suggestions for the best way to implement -hash such that it meets both these conditions and yet is...

Automatically generate MD5 and display.

On a page that automatically lists several small files (~100-500kb) that are contained in a specific folder, is there a way using VBScript to automatically generate MD5 hashes of each file and display it on the page? Cliff notes: Can I generate an MD5 hash of a file on the server machine? ...

urlsafe_b64encode always ends in '=' ?:

I think this must be a stupid question, but why do the results of urlsafe_b64encode() always end with a '=' for me? '=' isn't url safe? from random import getrandbits from base64 import urlsafe_b64encode from hashlib import sha256 from time import sleep def genKey(): keyLenBits = 64 a = str(getrandbits(keyLenBits)) b = urlsafe...

Very low cost hash function

Hello, I need a hash function for a Look Up table, so that if my values are from 0 to N, I need a hash function that give me a value from 0 to n, being n << N. Another piece of information is that I already know N in advance. I have been investigatinv about different low cost hash functions and I have found only this: h = z mod n ran...

How to generate a hash value in J2ME?

How can I generate hash value for a byte array, in J2ME? It doesn't have to be very very secure but it should be fast. ...

Is there a builtin "hash to string" in Perl?

I'm coming to learn Perl from a Python background where the following hash-to-string conversion is built in to the language: >>> d = {'a': 1, 'b': 2, 'c': 3} >>> str(d) "{'a': 1, 'c': 3, 'b': 2}" Is there a builtin and/or module that has a subroutine with output along the lines of: "('a' => 1, 'b' => 2, 'c' => 3)" Strangely, a web ...

Is there a java hash structure with keys only and no values?

I'm looking for a structure which hashes keys without requiring a value. When queried, it should return true if the key is found and false otherwise. I'm looking for something similar to Hashtable<MyClass, Boolean> except insertion requires only a key and queries only ever return true or false, never null. ...

Hash Code and Checksum - what's the difference?

My understanding is that a hash code and checksum are similar things - a numeric value, computed for a block of data, that is relatively unique. i.e. The probability of two blocks of data yielding the same numeric hash/checksum value is low enough that it can be ignored for the purposes of the application. So do we have two words for t...

Can you convert the output of php crypt() to valid MD5?

I have some strings that have been encrypted using the php function crypt() http://uk2.php.net/crypt The outputs look something like this: $1$Vf/.4.1.$CgCo33ebiHVuFhpwS.kMI0 $1$84..vD4.$Ps1PdaLWRoaiWDKCfjLyV1 $1$or1.RY4.$v3xo04v1yfB7JxDj1sC/J/ While I believe crypt() is using the MD5 algorithm, the outputs are not valid MD5 hashes. ...

Can I be sure the built-in hash for a given string is always the same?

I am getting a string hash like this: string content = "a very long string"; int contentHash = content.GetHashCode(); I am then storing the hash into a dictionary as key mapping to another ID. This is useful so I don't have to compare big strings during default dictionary hash computation but I can just fish the ID from the dictionary...

ObjectIDGenerator vs. HashSet<T>

Can anyone tell me how is ObjectIDGenerator better (worse?) then using HashSet when traversing an hierarchy of objects (that might be recurvise/circular), and not wanting to traverse the same object twice? ...

How do I create a unix password hash with php

I'm trying to create system users with a php script securely, In that, I'd like to be able to hash the password with the php script, so that their password shows up nowhere in the bash history. How to I take a string, and hash it so it is a unix password hash? $UX_PW = some_function('my_password'); exec("useradd -p $UX_PW newusername")...

What is the difference between a 'character' and an 'octet'?

I see the term 'octet' popping up in literature about nonces for hashing, and it seems to be synonymous with 'character', although there is a kind of pattern to how the words are used. This leads me to believe that there is a formal distinction between the two. If anyone could enlighten me to what it is, I'd appreciate it. (and please,...

(Ruby) How can I get a reference to a method?

Is it possible in Ruby to get a reference to methods of an object ( I would like to know if this can be done without procs/lambdas ) , for example , consider the following code : class X def initialize @map = {} setup_map end private def setup_map # @map["a"] = get reference to a method # @map["b"] = get refere...

Ruby 1.8: Hash.select not return hash but array (better way to do this?)

In some scenario of Ruby 1.8. If i have a hash # k is name, v is order foo = { "Jim" => 1, "bar" => 1, "joe" => 2} sortedByValues = foo.sort {|a, b| a[1] <==> b[1]} #sortedByValues is an array of array not longer a hash! sortedByValues.keys.join ',' my workaround is to make method to_hash for Array class. class Array def to_hash(&bl...