Group
Extension

Dancer-Session-Redis-JSON/lib/Dancer/Session/Redis/JSON/Signature.pm

use strict;
use warnings;
package Dancer::Session::Redis::JSON::Signature;

# Adapted from https://github.com/visionmedia/node-cookie-signature

use Digest::SHA qw(hmac_sha256_base64 sha1_hex);
use Function::Parameters qw(:strict);

use Exporter;
our @ISA       = qw(Exporter);
our @EXPORT_OK = qw(sign unsign);

fun sign(Str $val, Str $secret) {
    my $mac = hmac_sha256_base64($val, $secret);
    $mac =~ s/\=+$//;

    return "s:$val.$mac";
}

fun unsign(Str $val, Str $secret) {
    $val =~ s/^s://;

    my ($str, $id) = split /\./, $val;
    my $mac = sign($str, $secret);

    return $str
        if sha1_hex($mac) eq sha1_hex("s:$val");
}

1;

__END__

=pod

=head1 NAME

Dancer::Session::Redis::JSON::Signature

=head1 VERSION

version 0.001

=head1 AUTHOR

Forest Belton <forest@homolo.gy>

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2014 by Forest Belton.

This is free software, licensed under:

  The MIT (X11) License

=cut


Powered by Groonga
Maintained by Kenichi Ishigaki <ishigaki@cpan.org>. If you find anything, submit it on GitHub.