#1
  1. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,571
    Rep Power
    595

    Create Hash of Just Alphanumeric


    I am trying to create a hash (I can use any algorithm) but I need only alphanumeric codes in the resulting string. I can do this in Java but I can't come up with a way to do the same thing in PHP. Can someone help? TIA.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,247
    Rep Power
    9400
    Depends on the hashing algorithm, but if you have binary output then bin2hex() or base64_encode() are the best solutions.
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    base64 returns only alphanums, so just base64 the resulting hash
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,571
    Rep Power
    595
    I assume you mean base64_encode. That is what I am using but it returns alphanumeric and special characters.

    I am using the MD5 algorithm.

    Comments on this post

    • requinix agrees : oh right, I forgot about those two
    Last edited by gw1500se; October 2nd, 2012 at 05:07 PM.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7170
    Unless you pass a true value as the second argument to md5, the value it gives you is going to be alphanumeric already. By default the function returns a hex representation of the hash.

    If the binary hash is coming from some external system, you could either hash it a second time to get an alphanumeric value, or you could manually convert it to a hex representation using pack and substr.

    The maximum safe integer size in PHP is 4 bytes, and a binary md5 hash is 20 bytes, so you won't be able to pass it through either pack or bin2hex in one pass. However, I don't think bin2hex will pad the returned value, while I'm pretty sure pack does.

    base64 encoding using non-alphanumeric characters, but any encoding under base62 would work.
    Last edited by E-Oreo; October 2nd, 2012 at 06:40 PM.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,571
    Rep Power
    595
    I started out that way and was getting 32 bytes (I thought I read it was supposed to be 40). Essentially I am trying to get the hash size to a minimum. If I have to I will live with 32 but I'd like to have 20. This does not have to be a strong encryption. Strong obfuscation is sufficient.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #7
  13. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7170
    Sorry 20 was a mistake, md5 is actually 16 bytes. However, the ASCII character hex (ie base 16) representation of a 16 byte string is 32 bytes long. Each raw byte requires two ASCII characters to represent in base 16, and each ASCII character is 2 bytes.

    sha1 is 20 bytes raw, 40 bytes represented in ASCII base 16.

    The absolute minimum representation would be base 62 I think, given that there are only 62 alphanumeric characters. I don't believe there is a built in method for computing base 62, but it's possible to do in user-space and there are probably libraries for it already. Also since you know the length will always be exactly 16 bytes you could omit the padding characters.

    Base encoding is hardly obfuscation, I definitely would not classify it as strong. It's pretty easy to recognize base encoding from the padding characters that often follow it.
    Last edited by E-Oreo; October 2nd, 2012 at 07:25 PM.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  14. #8
  15. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,247
    Rep Power
    9400
    Base 62 will take just as much space as base 64: it requires 5.95 bits and you can't have a fraction of a bit. But it might compress slightly more (given an appropriate algorithm) due to the bias towards certain bits being on or off (eg, the 2^5 bit will be off the most at 51.6% of the time).

    The next step up would be base-128 but that's just crazy.

    The two extremes:
    1. You care more about space than representation, in which case you should stick with the binary form. But you don't want this.
    2. You care more about the representation, in which case base 64 is the best compromise between encoded size and character set: printable and pretty-much-everything-safe characters A-Z, a-z, 0-9, and / and + (IIRC), with only a 33% increase in length. For comparison bin2hex() will give you only a-z, 0-9 but the output is twice as long.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,571
    Rep Power
    595
    Got it. Thanks.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  18. #10
  19. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7170
    Yeah the point of using base62 would not really be to save space over base64, it would just be to eliminate the need to use non-alphanumeric characters. Base62 may actually end up taking more space than base62, but less than base 16.

    EDIT:
    Actually, base62 should take the same amount of space as base32 I believe, for the reasons mentioned by req, and it may be easier to find a base32 script than a base62 script.
    Last edited by E-Oreo; October 2nd, 2012 at 07:43 PM.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  20. #11
  21. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,247
    Rep Power
    9400
    Uh... what?

    Originally Posted by E-Oreo
    Base62 may actually end up taking more space than base62, but less than base 16.
    Originally Posted by E-Oreo
    Actually, base62 should take the same amount of space as base32 I believe, for the reasons mentioned by req

IMN logo majestic logo threadwatch logo seochat tools logo