From WikiChip
Editing mirc/identifiers/$hash
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
This page supports semantic in-text annotations (e.g. "[[Is specified as::World Heritage Site]]") to build structured and queryable content provided by Semantic MediaWiki. For a comprehensive description on how to use annotations or the #ask parser function, please have a look at the getting started, in-text annotation, or inline queries help pages.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
{{mirc title|$hash Identifier}} | {{mirc title|$hash Identifier}} | ||
− | The '''$hash''' identifier calculates a simple hash of the supplied text. The hash is shown as | + | The '''$hash''' identifier calculates a simple hash of the supplied text. The hash is shown as a decimal number in the range from 0 to 2^N-1 where N is the number of bits ranging from 2-32. Except for legacy scripts, you should probably avoid using this hash for reasons explained in the Notes section. It appears that $hash() is the method used by /hadd to put an item into one of the hash table's "buckets". |
== Synopsis == | == Synopsis == | ||
<pre>$hash(<text>,<N>)</pre> | <pre>$hash(<text>,<N>)</pre> | ||
+ | |||
== Switches == | == Switches == | ||
None | None | ||
+ | |||
== Parameters == | == Parameters == | ||
{{ArgsList | {{ArgsList | ||
| text | text string or %variable to be hashed | | text | text string or %variable to be hashed | ||
− | | B | Bit length for the returned hash. $hash returns $null if B is a number outside the range 2-32, and returns the original string if the B parameter isn't used. Returned hash is | + | | B | Bit length for the returned hash. $hash returns $null if B is a number outside the range 2-32, and returns the original string if the B parameter isn't used. Returned hash is a decimal number in the range 0 to 2^B -1. |
}} | }} | ||
== Properties == | == Properties == | ||
None | None | ||
== Example == | == Example == | ||
+ | |||
<source lang="mIRC">//echo -a The hash is $hash(test,32) | <source lang="mIRC">//echo -a The hash is $hash(test,32) | ||
returns: The hash is 1702094848</source> | returns: The hash is 1702094848</source> | ||
+ | |||
== Notes == | == Notes == | ||
+ | |||
The 'fakehash' alias below is based on the code posted here: https://forums.mirc.com/ubbthreads.php/topics/98371/Re:_$hash_function#Post98371 | The 'fakehash' alias below is based on the code posted here: https://forums.mirc.com/ubbthreads.php/topics/98371/Re:_$hash_function#Post98371 | ||
Line 47: | Line 52: | ||
returns: 4952B000 vs 4952B100</source> | returns: 4952B000 vs 4952B100</source> | ||
− | * 4. It's easy to create duplicate hashes from different inputs: | + | * 4. It's easy to create duplicate hashes from different inputs, especially when the length of the inputs are a multiple of 3: |
<source lang="mIRC">//echo -a $base($hash(ABCDEF,32),10,16) / $base($hash(ABDDEE,32),10,16)</source> | <source lang="mIRC">//echo -a $base($hash(ABCDEF,32),10,16) / $base($hash(ABDDEE,32),10,16)</source> | ||
− | + | Instead of using $hash, you would be better off using other substitutes. For example, if you need it to be a decimal number with a variable 1-32 number of bits, use $crc then convert to decimal then reduce the number of bits: | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | <source lang="mIRC">alias crchash { return $calc( $base($crc($1,0),16,10) % (2^$iif($2 isnum 1-32,$gettok($2,1,46),32)) ) } | |
− | |||
− | <source lang="mIRC">alias crchash { return $calc( $base($crc($1,0),16,10) % (2^$iif( | ||
//echo -a $base($crchash(mIRC,32),10,16) vs $base($crchash(mIRD,32),10,16) | //echo -a $base($crchash(mIRC,32),10,16) vs $base($crchash(mIRD,32),10,16) | ||
Line 68: | Line 64: | ||
</source> | </source> | ||
− | If | + | If the hash needs to be based on a crypto-level hash, or needs more than 32 bits, use up-to-52 bits from $sha1 or $sha512 instead of from $crc. |
− | + | <source lang="mIRC"> | |
− | + | alias sha1hash { | |
− | |||
− | |||
− | <source lang="mIRC">alias sha1hash { | ||
var %sha1 $sha1($1) , %offset $base($right(%sha1,1),16,10) + 1 , %hash13 $base($mid(%sha1,%offset,13),16,10) | var %sha1 $sha1($1) , %offset $base($right(%sha1,1),16,10) + 1 , %hash13 $base($mid(%sha1,%offset,13),16,10) | ||
return $calc( %hash13 % (2^$iif($2 isnum 1-52,$gettok($2,1,46),32)) ) | return $calc( %hash13 % (2^$iif($2 isnum 1-52,$gettok($2,1,46),32)) ) | ||
Line 81: | Line 74: | ||
//echo -a $base($sha1hash(mIRC,52),10,16) vs $base($sha1hash(mIRD,52),10,16) | //echo -a $base($sha1hash(mIRC,52),10,16) vs $base($sha1hash(mIRD,52),10,16) | ||
returns: 9AB6235ED89DF vs D2F9CD20CA42B | returns: 9AB6235ED89DF vs D2F9CD20CA42B | ||
− | |||
− | |||
− | |||
− | |||
− | |||
</source> | </source> | ||
Line 156: | Line 144: | ||
bits: 4 #randoms: 10000 stringlen: 8 input range a-z distribution: 624 618 617 627 610 638 624 626 602 635 612 650 632 600 627 658</source> | bits: 4 #randoms: 10000 stringlen: 8 input range a-z distribution: 624 618 617 627 610 638 624 626 602 635 612 650 632 600 627 658</source> | ||
− | $crc is not of cryptographic quality, but at least it has a good distribution, and hash functions don't | + | $crc is not of cryptographic quality, but at least it has a good distribution, and hash functions don't need a 1-way feature, they just need to be fast. A good distribution is not proof of a good hash, since even a repeating pattern of 1-through-10 has that. |
== Compatibility == | == Compatibility == | ||
{{mIRC compatibility|5.4}} | {{mIRC compatibility|5.4}} | ||
== See also == | == See also == | ||
* {{mIRC|$crc}} | * {{mIRC|$crc}} | ||
− | |||
* {{mIRC|$md5}} | * {{mIRC|$md5}} | ||
* {{mIRC|$sha1}} | * {{mIRC|$sha1}} |