The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Digest::Trivial - Trivial but fast ways to map strings to small integers

SYNOPSIS

  use Digest::Trivial;

  say trivial_x "hello, world";       #   12
  say trivial_s "hello, world";       #  136

DESCRIPTION

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 xorring the code points, returning the resulting value.

trivial_s

Calculates the digest by adding the code points, returning the sum module 256.

Caveats

There are a few things to consider.

  • Since adding and xorring 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.

  • The functions look at the strings bytewise. That is, a string may be mapped to a different integer depending whether it's UTF-8 encoded or not.

  • If there are no non-ASCII characters present in the string, trivial_x will not return an integer above 127. Since xorring a value with itself returns 0, trivial_x effectively takes the digest of the characters that appear an odd times in the string.

Exports

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.

IMPLEMENTATION

The algorithms have been implemented in XS for efficiency reasons.

BUGS

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.

SEE ALSO

Digest::MD5, Digest::MD4, Digest::SHA1, etc.

DEVELOPMENT

The current sources of this module are found on github, git://github.com/Abigail/Digest--Trivial.git.

AUTHOR

Abigail, mailto:digest-trivial@abigail.be.

COPYRIGHT and LICENSE

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.

INSTALLATION

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.