Hash Function

This is a discussion on Hash Function within the C++ Programming forums, part of the General Programming Boards category; I came up with a string hashing algorithm which I haven't seen before, and I was hoping to get some ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2,421

    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.

    Code:
    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
    Location
    Singapore
    Posts
    21,734
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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
    Posts
    8,893
    Is the modulo operation truly necessary? Modulo is still among the slowest integer operations you can execute.
    All the buzzt!
    CornedBee

    "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
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

    --
    Mats
    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, 07:55 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21