Algorithm-CouponCode
view release on metacpan or search on metacpan
lib/Algorithm/CouponCode.pm view on Meta::CPAN
"$a$c$b$d",
"$a$b$d$c",
) {
next if $code eq $orig;
if(_checkdigit_alg_1(substr($code, 0, 3), $pos) eq substr($code, 3, 1)) {
return 1;
}
}
return 0;
}
1;
__END__
=pod
=head1 SYNOPSIS
use Algorithm::CouponCode qw(cc_generate cc_validate);
print cc_generate(parts => 3); # generate a 3-part code
my $valid_code = cc_validate(code => $code, parts => 3) or die "Invalid code";
=head1 DESCRIPTION
A 'Coupon Code' is made up of letters and numbers grouped into 4 character
'parts'. For example, a 3-part code might look like this:
1K7Q-CTFM-LMTC
Coupon Codes are random codes which are easy for the recipient to type
accurately into a web form. An example application might be to print a code on
a letter to a customer who would then enter the code as part of the
registration process for web access to their account.
Features of the codes that make them well suited to manual transcription:
=over 4
=item *
The codes are not case sensitive.
=item *
Not all letters and numbers are used, so if a person enters the letter 'O' we
can automatically correct it to the digit '0' (similarly for I => 1, S => 5, Z
=> 2).
=item *
The 4th character of each part is a checkdigit, so client-side scripting can
be used to highlight parts which have been mis-typed, before the code is even
submitted to the application's back-end validation.
=item *
The checkdigit algorithm takes into account the position of the part being
keyed. So for example '1K7Q' might be valid in the first part but not in the
second so if a user typed the parts in the wrong boxes then their error could
be highlighted.
=item *
The code generation algorithm avoids 'undesirable' codes. For example any code
in which transposed characters happen to result in a valid checkdigit will be
skipped. Any generated part which happens to spell an 'inappropriate' 4-letter
word (e.g.: 'P00P') will also be skipped.
=back
The Algorithm-CouponCode distribution includes a Javascript implementation of
the validator function, in the form of a jQuery plugin. You can include this
in your web application to do client-side validation and highlighting of
errors.
I<Note> the cc_validate function and the Javascript plugin only validate that
the code is 'well-formed' (i.e.: each part has a valid checkdigit). Checking
whether the code is in fact 'valid' is left up to your application and would
typically involve looking for the code in a database. If you use the
Javascript plugin, you might choose to tweak the CSS to keep the red
highlighting of checksum errors but remove the green highlighting which might
imply the code was correct.
=head2 Randomness and Uniqueness
The code returned by C<cc_generate()> is random, but not necessarily unique.
If your application requires unique codes, it is your responsibility to
avoid duplicates (for example by using a unique index on your database column).
The codes are generated using a SHA1 cryptographic hash of a plaintext. If you
do not supply a plaintext, one will be generated for you (using /dev/urandom if
available or Perl's C<rand()> function otherwise). In the event that an
'inappropriate' code is created, the generated hash will be used as a
plaintext input for generating a new hash and the process will be repeated.
Each 4-character part encodes 15 bits of random data, so a 3-part code will
incorporate 45 bits making a total of 2^45 (approximately 35 trillion) unique
codes.
=head1 EXPORTS
The following functions can be exported from the C<Algorithm::CouponCode>
module. No functions are exported by default.
=head2 cc_generate( options )
Returns a coupon code as a string of 4-character parts separated by '-'
characters. The following optional named parameters may be supplied:
=over 4
=item parts
The number of parts desired. Must be a number in the range 1 - 6. Default is
3.
=item plaintext
( run in 0.763 second using v1.01-cache-2.11-cpan-39bf76dae61 )