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's possible that $hash() was once the method used by /hadd to put an item into one of the hash table's "buckets", but that was not the case at the point when it was changed to use 'modified FNV-1A'. |
== Synopsis == | == Synopsis == | ||
Line 9: | Line 9: | ||
{{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 == | ||
Line 47: | Line 47: | ||
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> | ||
− | + | * 5. One of the properties of a good hash function is that it should be reasonably fast. However $hash is excrutiatingly slow compared to $crc. | |
− | |||
− | * 5. One of the properties of a good hash function is that it should be reasonably fast. However $hash is | ||
<source lang="mIRC"> | <source lang="mIRC"> | ||
//var %string $str(a,8192), %reps 1000, %t $ticks | while (%reps) { noop $crc(%string,0) | dec %reps } | echo -a ticks: $calc($ticks - %t) | //var %string $str(a,8192), %reps 1000, %t $ticks | while (%reps) { noop $crc(%string,0) | dec %reps } | echo -a ticks: $calc($ticks - %t) | ||
Line 60: | Line 58: | ||
As of v7.56, on a computer where the 1000 repetitions takes $crc approx 1/10th of a second, the same work using $hash takes longer than 30 seconds. | As of v7.56, on a computer where the 1000 repetitions takes $crc approx 1/10th of a second, the same work using $hash takes longer than 30 seconds. | ||
− | Instead of using $hash, you would be better off using other substitutes. For example, if you need it to be | + | 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($$2 isnum 1-32,$gettok($2,1,46),32)) ) } | ||
Line 68: | Line 66: | ||
</source> | </source> | ||
− | + | 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. | |
− | |||
− | |||
− | |||
− | 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 { | ||
Line 156: | Line 150: | ||
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}} |