Digest::Trivial - Trivial but fast ways to map strings to small integers
use Digest::Trivial; say trivial_x "hello, world"; # 12 say trivial_s "hello, world"; # 136
The module provides 2 methods that take a string as input, and return an integer (the digest) between 0 and 255 inclusive. The goal is to provide functions with algorithms, that are fast, repeatable, and map to all possible integers in the range roughly evenly. They aren't cryptically secure; the returned integer is easily guessable, and it's trivial to create a string that maps to a certain integer.
The following functions are available:
trivial_x
Calculates the digest by xor
ring the code points, returning the resulting value.
trivial_s
Calculates the digest by adding the code points, returning the sum module 256.
There are a few things to consider.
xor
ring are symmetric operations, two strings that only differ by the order in which the characters appear in the string will be mapped to the same number.trivial_x
will not return an integer above 127. Since xor
ring a value with itself returns 0, trivial_x
effectively takes the digest of the characters that appear an odd times in the string.By default, Digest::Trivial
exports both trivial_x
and trivial_s
. Use an explicite (possibly empty) import list if you want a subset of the default.
The algorithms have been implemented in XS for efficiency reasons.
If the argument of trivial_s
or trivial_x
contains a NUL
byte, only the part of the string preceeding the NUL
byte is used to calculate the digest.
Digest::MD5
, Digest::MD4
, Digest::SHA1
, etc.
The current sources of this module are found on github, git://github.com/Abigail/Digest--Trivial.git.
Abigail, mailto:digest-trivial@abigail.be.
Copyright (C) 2009 by Abigail.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
To install this module, run, after unpacking the tar-ball, the following commands:
perl Makefile.PL make make test make install
You will need a C compiler to install the module.