IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Modules Perl Discussion :

Hachage MD5 en Perl


Sujet :

Modules Perl

  1. #1
    Candidat au Club
    Profil pro
    Étudiant
    Inscrit en
    Septembre 2009
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Septembre 2009
    Messages : 5
    Points : 4
    Points
    4
    Par défaut Hachage MD5 en Perl
    bonjour!
    bon voila j'ai besoin programme de hachage de type MD5 en perl
    j'ai trouvé ça

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
     
    # Derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm
     
    use strict;
    package md5;
    require Exporter;
    @md5::ISA = qw(Exporter);
    @md5::EXPORT = qw(md5 md5_init md5_update md5_final);
     
    # MD5 context
    my @state;      # array of four integers (A, B, C, D)
    my @count;      # number of bits, modulo 2^64, lsb first
    my $buffer;     # accumulates bytes until we get a 64-byte block to process
     
    my $PADDING = "\200\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" .
                    "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" .
                    "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" .
                    "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
     
     
    # Given '$str', returns digest of '$str'
    sub md5 # ($str)
    {
            my ($str) = @_;
     
            my $digest;
     
            md5_init();
            md5_update($str);
            $digest = md5_final();
            return $digest;
    }
     
     
    # Finishes the MD5 calculation and returns the digest
    sub md5_final
    {
            my $bits;
            my $index;
            my $pad_len;
     
            # Save number of bits
            $bits = md5_encode(@count);
     
            # Pad out to 56 mod 64.
            $index = ($count[0] >> 3) & 0x3f;
            if ($index < 56)
              {
              $pad_len = 56 - $index;
              }
            else
              {
              $pad_len = 120 - $index;
              }
            md5_update(substr($PADDING,0,$pad_len));
     
            # Append length (before padding)
            md5_update($bits);
     
            return md5_encode(@state);
    }
     
     
    # Initialize MD5 context to start a new digest
    sub md5_init
    {
            @count = (0,0);
            # magic initialization constants
            @state = (0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476);
            $buffer = '';
    }
     
     
    # Process another block of message, updating context
    sub md5_update # ($input)
    {
            my($input) = @_;
     
            my $i;
            my $index;
            my $input_len;
            my $part_len;
     
            $input_len = length($input);
     
            # Compute number of bytes mod 64
            $index = (($count[0] >> 3) & 0x3F);
     
            # Update number of bits
            $count[0] = $count[0] + ($input_len << 3);
            if ($count[0] != ($count[0] & 0xffffffff))      # overflow
              {
              $count[0] = 0;
              $count[1] = $count[1] + 1;
              }
            $count[1] = $count[1] + ($input_len >> 29);
     
            $part_len = 64 - $index;
     
            # Transform as many times as possible.
     
            if ($input_len < $part_len)
              {
              $i = 0;
              }
            else
              {
              substr($buffer,$index,$part_len) = substr($input,0,$part_len);
              md5_transform($buffer);
     
              for ($i = $part_len; $i + 63 < $input_len; $i += 64)
                {
                md5_transform(substr($input,$i,64));
                }
              $index = 0;
              }
     
            # Buffer remaining input
            substr($buffer,$index,($input_len - $i)) =
                                              substr($input,$i,($input_len - $i));
    }
     
     
    sub add_unsigned # ($a1,...,$an)
    {
            my $addend;
            my $sum;
     
            $sum = 0;
            foreach $addend (@_)
              {
              $sum = (($sum & 0x7fffffff) + ($addend & 0x7fffffff)) ^
                      ($sum & 0x80000000) ^ ($addend & 0x80000000);
              }
     
            return $sum;
    }
     
     
    sub FF # ($a,$b,$c,$d,$x,$s,$ac)
    {
            my($a,$b,$c,$d,$x,$s,$ac) = @_;
     
            $a = add_unsigned($a,(($b & $c) | (~$b & $d)),$x,$ac);
            $a = rotate_left($a,$s);
            return add_unsigned($a,$b);
    }
     
     
    sub GG # ($a,$b,$c,$d,$x,$s,$ac)
    {
            my($a,$b,$c,$d,$x,$s,$ac) = @_;
     
            $a = add_unsigned($a,(($b & $d) | ($c & ~$d)),$x,$ac);
            $a = rotate_left($a,$s);
            return add_unsigned($a,$b);
    }
     
     
    sub HH # ($a,$b,$c,$d,$x,$s,$ac)
    {
            my($a,$b,$c,$d,$x,$s,$ac) = @_;
     
            $a = add_unsigned($a,($b ^ $c ^ $d),$x,$ac);
            $a = rotate_left($a,$s);
            return add_unsigned($a,$b);
    }
     
     
    sub II # ($a,$b,$c,$d,$x,$s,$ac)
    {
            my($a,$b,$c,$d,$x,$s,$ac) = @_;
     
            $a = add_unsigned($a,($c ^ ($b | ~$d)),$x,$ac);
            $a = rotate_left($a,$s);
            return add_unsigned($a,$b);
    }
     
     
    # Returns packed version of $input in array of integers
    # length of $input must be a multiple of 4
    sub md5_decode # ($input)
    {
            my($input) = @_;
     
            my $i;
            my $j;
            my $len;
            my @output;
     
            $len = length($input);
            $i = 0;
            $j = 0;
            while ($j < $len)
              {
              $output[$i] = ord(substr($input,$j,1)) |
                          (ord(substr($input,$j+1,1)) <<  8) |
                          (ord(substr($input,$j+2,1)) << 16) |
                          (ord(substr($input,$j+3,1)) << 24);
              $i = $i + 1;
              $j = $j + 4;
              }
     
            return @output;
    }
     
     
    # Returns character version of (array of integers) @input
    sub md5_encode # (@input)
    {
            my(@input) = @_;
     
            my $output;
            my $i;
            my $len;
     
            $len = $#input + 1;
            $i = 0;
            $output = '';
            while ($i < $len)
              {
              $output = $output . chr( $input[$i]        & 0xff);
              $output = $output . chr(($input[$i] >>  8) & 0xff);
              $output = $output . chr(($input[$i] >> 16) & 0xff);
              $output = $output . chr(($input[$i] >> 24) & 0xff);
              $i = $i + 1;
              }
     
            return $output;
    }
     
     
    # MD5 basic transformation. Transforms state based on $block.
    # $block must contain exactly 64 bytes.
    sub md5_transform # ($block)
    {
            my($block) = @_;
     
            my $a = $state[0];
            my $b = $state[1];
            my $c = $state[2];
            my $d = $state[3];
            my @x;
     
            @x = md5_decode($block);
     
    # Round 1
            $a = FF($a,$b,$c,$d,$x[ 0], 7,0xd76aa478); # 1
            $d = FF($d,$a,$b,$c,$x[ 1],12,0xe8c7b756); # 2
            $c = FF($c,$d,$a,$b,$x[ 2],17,0x242070db); # 3
            $b = FF($b,$c,$d,$a,$x[ 3],22,0xc1bdceee); # 4
            $a = FF($a,$b,$c,$d,$x[ 4], 7,0xf57c0faf); # 5
            $d = FF($d,$a,$b,$c,$x[ 5],12,0x4787c62a); # 6
            $c = FF($c,$d,$a,$b,$x[ 6],17,0xa8304613); # 7
            $b = FF($b,$c,$d,$a,$x[ 7],22,0xfd469501); # 8
            $a = FF($a,$b,$c,$d,$x[ 8], 7,0x698098d8); # 9
            $d = FF($d,$a,$b,$c,$x[ 9],12,0x8b44f7af); # 10
            $c = FF($c,$d,$a,$b,$x[10],17,0xffff5bb1); # 11
            $b = FF($b,$c,$d,$a,$x[11],22,0x895cd7be); # 12
            $a = FF($a,$b,$c,$d,$x[12], 7,0x6b901122); # 13
            $d = FF($d,$a,$b,$c,$x[13],12,0xfd987193); # 14
            $c = FF($c,$d,$a,$b,$x[14],17,0xa679438e); # 15
            $b = FF($b,$c,$d,$a,$x[15],22,0x49b40821); # 16
     
    # Round 2
            $a = GG($a,$b,$c,$d,$x[ 1], 5,0xf61e2562); # 17
            $d = GG($d,$a,$b,$c,$x[ 6], 9,0xc040b340); # 18
            $c = GG($c,$d,$a,$b,$x[11],14,0x265e5a51); # 19
            $b = GG($b,$c,$d,$a,$x[ 0],20,0xe9b6c7aa); # 20
            $a = GG($a,$b,$c,$d,$x[ 5], 5,0xd62f105d); # 21
            $d = GG($d,$a,$b,$c,$x[10], 9, 0x2441453); # 22
            $c = GG($c,$d,$a,$b,$x[15],14,0xd8a1e681); # 23
            $b = GG($b,$c,$d,$a,$x[ 4],20,0xe7d3fbc8); # 24
            $a = GG($a,$b,$c,$d,$x[ 9], 5,0x21e1cde6); # 25
            $d = GG($d,$a,$b,$c,$x[14], 9,0xc33707d6); # 26
            $c = GG($c,$d,$a,$b,$x[ 3],14,0xf4d50d87); # 27
            $b = GG($b,$c,$d,$a,$x[ 8],20,0x455a14ed); # 28
            $a = GG($a,$b,$c,$d,$x[13], 5,0xa9e3e905); # 29
            $d = GG($d,$a,$b,$c,$x[ 2], 9,0xfcefa3f8); # 30
            $c = GG($c,$d,$a,$b,$x[ 7],14,0x676f02d9); # 31
            $b = GG($b,$c,$d,$a,$x[12],20,0x8d2a4c8a); # 32
     
    # Round 3
            $a = HH($a,$b,$c,$d,$x[ 5], 4,0xfffa3942); # 33
            $d = HH($d,$a,$b,$c,$x[ 8],11,0x8771f681); # 34
            $c = HH($c,$d,$a,$b,$x[11],16,0x6d9d6122); # 35
            $b = HH($b,$c,$d,$a,$x[14],23,0xfde5380c); # 36
            $a = HH($a,$b,$c,$d,$x[ 1], 4,0xa4beea44); # 37
            $d = HH($d,$a,$b,$c,$x[ 4],11,0x4bdecfa9); # 38
            $c = HH($c,$d,$a,$b,$x[ 7],16,0xf6bb4b60); # 39
            $b = HH($b,$c,$d,$a,$x[10],23,0xbebfbc70); # 40
            $a = HH($a,$b,$c,$d,$x[13], 4,0x289b7ec6); # 41
            $d = HH($d,$a,$b,$c,$x[ 0],11,0xeaa127fa); # 42
            $c = HH($c,$d,$a,$b,$x[ 3],16,0xd4ef3085); # 43
            $b = HH($b,$c,$d,$a,$x[ 6],23, 0x4881d05); # 44
            $a = HH($a,$b,$c,$d,$x[ 9], 4,0xd9d4d039); # 45
            $d = HH($d,$a,$b,$c,$x[12],11,0xe6db99e5); # 46
            $c = HH($c,$d,$a,$b,$x[15],16,0x1fa27cf8); # 47
            $b = HH($b,$c,$d,$a,$x[ 2],23,0xc4ac5665); # 48
    # Round 4
            $a = II($a,$b,$c,$d,$x[ 0], 6,0xf4292244); # 49
            $d = II($d,$a,$b,$c,$x[ 7],10,0x432aff97); # 50
            $c = II($c,$d,$a,$b,$x[14],15,0xab9423a7); # 51
            $b = II($b,$c,$d,$a,$x[ 5],21,0xfc93a039); # 52
            $a = II($a,$b,$c,$d,$x[12], 6,0x655b59c3); # 53
            $d = II($d,$a,$b,$c,$x[ 3],10,0x8f0ccc92); # 54
            $c = II($c,$d,$a,$b,$x[10],15,0xffeff47d); # 55
            $b = II($b,$c,$d,$a,$x[ 1],21,0x85845dd1); # 56
            $a = II($a,$b,$c,$d,$x[ 8], 6,0x6fa87e4f); # 57
            $d = II($d,$a,$b,$c,$x[15],10,0xfe2ce6e0); # 58
            $c = II($c,$d,$a,$b,$x[ 6],15,0xa3014314); # 59
            $b = II($b,$c,$d,$a,$x[13],21,0x4e0811a1); # 60
            $a = II($a,$b,$c,$d,$x[ 4], 6,0xf7537e82); # 61
            $d = II($d,$a,$b,$c,$x[11],10,0xbd3af235); # 62
            $c = II($c,$d,$a,$b,$x[ 2],15,0x2ad7d2bb); # 63
            $b = II($b,$c,$d,$a,$x[ 9],21,0xeb86d391); # 64
     
            $state[0] = add_unsigned($state[0],$a);
            $state[1] = add_unsigned($state[1],$b);
            $state[2] = add_unsigned($state[2],$c);
            $state[3] = add_unsigned($state[3],$d);
    }
     
     
    # rotate $x left $n bits.
    sub rotate_left # ($x,$n)
    {
            my($x,$n) = @_;
     
            return(($x << $n) | ($x >> (32 - $n)) & 0xffffffff);
    }
     
    1;      # for require
    ******************************************************

    mais ça ne marche pas, je n'ai pas trouvé l'emplacement des l'input ( c a d le chaine a entrer) et output ( "les caractères" après hachage de la chaine) et je suis debutant en perl
    merci

  2. #2
    Membre actif

    Profil pro
    Inscrit en
    Août 2009
    Messages
    156
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France

    Informations forums :
    Inscription : Août 2009
    Messages : 156
    Points : 211
    Points
    211
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    # Given '$str', returns digest of '$str'
    sub md5 # ($str)
    {
    Un simple appel du type:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    require "/cheminVersTonFichiermd5.pm";
    my $chaineEncodee = md5::md5("chaine a encoder");
    devrait te retourner ton résultat.

  3. #3
    Responsable Perl et Outils

    Avatar de djibril
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    19 820
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 19 820
    Points : 499 184
    Points
    499 184
    Par défaut
    Y a pas de raison de fourer son nez dans le code des modules. Il suffit d'installer et d'utiliser le module Digest::MD5 ou Digest::MD5::File

  4. #4
    Invité
    Invité(e)
    Par défaut
    Citation Envoyé par djibril Voir le message
    Y a pas de raison de fourer son nez dans le code des modules. Il suffit d'installer et d'utiliser le module Digest::MD5 ou Digest::MD5::File
    +1

    Perso, j'utiliserai SHA1.

Discussions similaires

  1. decrypter un hachage MD5 avec c#
    Par dalilnet dans le forum Général Dotnet
    Réponses: 9
    Dernier message: 13/11/2013, 17h37
  2. Appliquer la fonction de hachage MD5 à un texte
    Par 9tita dans le forum Sécurité
    Réponses: 2
    Dernier message: 01/05/2011, 16h13
  3. [MySQL] Hachage MD5 du mot de passe
    Par elixcyr dans le forum PHP & Base de données
    Réponses: 5
    Dernier message: 25/12/2009, 09h50
  4. Réponses: 9
    Dernier message: 24/06/2008, 22h21

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo