Sha256: 1fefb395e31dca31e5b2d970d2f75565e6ead9e7107d35e6ef0a5f6625d6d6f4

Contents?: true

Size: 1.29 KB

Versions: 73

Compression:

Stored size: 1.29 KB

Contents

package Crypto;

use strict;
use warnings;
use POSIX qw(fmod ceil);

sub new {
    my ($class, $plaintext) = @_;
    return bless { plaintext => $plaintext } => $class;
}

sub normalize_plaintext {
    my $self = shift;
    my $normalized = lc $self->{plaintext}; 
    $normalized =~ s/[^0-9a-zA-Z]//g;

    return $normalized;
}

sub size {
    my $self = shift;
    my $size = @_ > 0 ? sqrt(length $_[0]) : sqrt(length $self->normalize_plaintext);

    return  int($size) if fmod($size, 1) == 0;
    return ceil($size);
}

sub normalize_ciphertext {
    my $self = shift;
    join ' ', $self->_columns;
}

sub ciphertext {
    my $self = shift;
    join '', $self->_columns;
}

sub plaintext_segments {
    my $self = shift;
    return $self->_segmentize($self->normalize_plaintext, $self->size);           
}

sub _segmentize {
    my ($self, $to_segment, $size) = @_;
    [grep {$_} split /(.{$size})/, $to_segment];
}

sub _columns {
    my $self = shift;
    my $plaintext_segments = $self->plaintext_segments;
    my @columns;
    
    foreach my $index (0 .. @$plaintext_segments) {
        my $column;
        foreach my $seg ( @$plaintext_segments ) {
            $column .= substr $seg, $index, 1 if $index < length $seg;
        }
        push @columns, $column if $column;
    }
    @columns;
}

__PACKAGE__;

Version data entries

73 entries across 73 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/perl5/exercises/crypto-square/.meta/solutions/Crypto.pm
trackler-2.2.1.179 tracks/perl5/exercises/crypto-square/.meta/solutions/Crypto.pm
trackler-2.2.1.178 tracks/perl5/exercises/crypto-square/.meta/solutions/Crypto.pm
trackler-2.2.1.177 tracks/perl5/exercises/crypto-square/.meta/solutions/Crypto.pm
trackler-2.2.1.176 tracks/perl5/exercises/crypto-square/.meta/solutions/Crypto.pm
trackler-2.2.1.175 tracks/perl5/exercises/crypto-square/.meta/solutions/Crypto.pm
trackler-2.2.1.174 tracks/perl5/exercises/crypto-square/.meta/solutions/Crypto.pm
trackler-2.2.1.173 tracks/perl5/exercises/crypto-square/.meta/solutions/Crypto.pm
trackler-2.2.1.172 tracks/perl5/exercises/crypto-square/.meta/solutions/Crypto.pm
trackler-2.2.1.171 tracks/perl5/exercises/crypto-square/.meta/solutions/Crypto.pm
trackler-2.2.1.170 tracks/perl5/exercises/crypto-square/.meta/solutions/Crypto.pm
trackler-2.2.1.169 tracks/perl5/exercises/crypto-square/.meta/solutions/Crypto.pm
trackler-2.2.1.167 tracks/perl5/exercises/crypto-square/.meta/solutions/Crypto.pm
trackler-2.2.1.166 tracks/perl5/exercises/crypto-square/.meta/solutions/Crypto.pm
trackler-2.2.1.165 tracks/perl5/exercises/crypto-square/.meta/solutions/Crypto.pm
trackler-2.2.1.164 tracks/perl5/exercises/crypto-square/.meta/solutions/Crypto.pm
trackler-2.2.1.163 tracks/perl5/exercises/crypto-square/.meta/solutions/Crypto.pm
trackler-2.2.1.162 tracks/perl5/exercises/crypto-square/.meta/solutions/Crypto.pm
trackler-2.2.1.161 tracks/perl5/exercises/crypto-square/.meta/solutions/Crypto.pm
trackler-2.2.1.160 tracks/perl5/exercises/crypto-square/.meta/solutions/Crypto.pm