Thread: Hash Function

  1. #1
    Registered User
    Join Date
    Oct 2006

    Hash Function

    I came up with a string hashing algorithm which I haven't seen before, and I was hoping to get some opinions on whether it has merit as a usable hashing function for things like cryptography.

    unsigned long hash(const std::string& str)
      std::string::iterator it;
      unsigned long hashed = 0;
      for(it = str.begin(); it != str.end(); it++)
        hashed = ((hashed - (*it)) << ((*it) &#37; 7)) + (*it);
      return hashed;

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Algorithm aside, that certainly would not be good enough for use as a cryptographic hash since the output is limited to 64 bits, at most, on typical platforms. It would be far too vulnerable to a birthday attack.

    The algorithm itself looks like one of those used for hash tables.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Visalia, CA, USA
    It looks solid for making a hash table. Though for that purpose it looks like it may have some collisions with higher valued characters.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Is the modulo operation truly necessary? Modulo is still among the slowest integer operations you can execute.
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    Quote Originally Posted by CornedBee View Post
    Is the modulo operation truly necessary? Modulo is still among the slowest integer operations you can execute.
    You could get a similar result by & 7 - but it extends the range a little bit from 0..6 to 0..7 - I have no idea if that matters much here.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Hash function
    By g_p in forum C Programming
    Replies: 1
    Last Post: 05-29-2007, 05:49 AM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM