Skip to content
Snippets Groups Projects
Commit 5c98e1c3 authored by Guillaume ROUSSE's avatar Guillaume ROUSSE
Browse files

add OTP-based encryption/decryption functions

parent 93a067af
No related branches found
No related tags found
No related merge requests found
......@@ -7,8 +7,39 @@ use Digest::SHA;
use Digest::MD5;
use Encode;
use English qw(-no_match_vars);
use List::MoreUtils qw(pairwise);
use MIME::Base64;
use Template;
sub encrypt {
my ($string, $key) = @_;
my @string_chars = split(//, $string);
my @key_chars = split(//, $key);
return encode_base64(otp(\@string_chars, \@key_chars));
}
sub decrypt {
my ($string, $key) = @_;
my @string_chars = split(//, decode_base64($string));
my @key_chars = split(//, $key);
return otp(\@string_chars, \@key_chars);
}
sub otp {
my ($string, $key) = @_;
my @chars =
pairwise { chr(ord($a) ^ ord($b)) }
@$string,
@$key;
return join('', @chars);
}
# get SHA256 hash for a string
sub sha256_hash {
my ($s) = @_;
......
#!/usr/bin/perl
use strict;
use warnings;
use English qw(-no_match_vars);
use Test::More;
use AccountManager::Tools;
plan tests => 4;
my $key = AccountManager::Tools::generate_token(undef, 10);
my $secret = AccountManager::Tools::generate_password();
ok($key ne $secret, 'key and secret are random strings');
ok(length($key) == length($secret), 'key and secret have same size');
my $encrypted_secret = AccountManager::Tools::encrypt($secret, $key);
ok($encrypted_secret ne $secret, 'crypted_secret and secret are differents');
my $decrypted_secret = AccountManager::Tools::decrypt($encrypted_secret, $key);
ok($decrypted_secret eq $secret, 'decrypted_secret and secret are equals');
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment