source: perl/modules/Jabber/lib/Net/Jabber/Key.pm @ a8c55b5

release-1.9
Last change on this file since a8c55b5 was a8c55b5, checked in by David Benjamin <davidben@mit.edu>, 9 years ago
Use Digest::SHA in Jabber module instead of Digest::SHA1 The cool kids spell it without the 1 these days. More precisely, Digest::SHA1 no longer exists in precise. Also it's in perl itself these days. (We can just install Digest::SHA into the locker for the sysnames that need it.)
  • Property mode set to 100644
File size: 5.0 KB
Line 
1##############################################################################
2#
3#  This library is free software; you can redistribute it and/or
4#  modify it under the terms of the GNU Library General Public
5#  License as published by the Free Software Foundation; either
6#  version 2 of the License, or (at your option) any later version.
7#
8#  This library is distributed in the hope that it will be useful,
9#  but WITHOUT ANY WARRANTY; without even the implied warranty of
10#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11#  Library General Public License for more details.
12#
13#  You should have received a copy of the GNU Library General Public
14#  License along with this library; if not, write to the
15#  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16#  Boston, MA  02111-1307, USA.
17#
18#  Jabber
19#  Copyright (C) 1998-1999 The Jabber Team http://jabber.org/
20#
21##############################################################################
22
23package Net::Jabber::Key;
24
25=head1 NAME
26
27Net::Jabber::Key - Jabber Key Library
28
29=head1 SYNOPSIS
30
31  Net::Jabber::Key is a module that provides a developer easy access
32  to generating, caching, and comparing keys.
33
34=head1 DESCRIPTION
35
36  Key.pm is a helper module for the Net::Jabber::Transport.  When the
37  Transport talks to a Client it sends a key and expects to get that
38  key back from the Client.  This module provides an API to generate,
39  cache, and then compare the key send from the Client.
40
41=head2 Basic Functions
42
43    $Key = Net::Jabber::Key->new();
44
45    $key = $Key->Generate();
46
47    $key = $Key->Create("bob\@jabber.org");
48
49    $test = $Key->Compare("bob\@jabber.org","some key");
50
51=head1 METHODS
52
53=head2 Basic Functions
54
55    new(debug=>string,       - creates the Key object.  debug should
56        debugfh=>FileHandle,   be set to the path for the debug
57        debuglevel=>integer)   log to be written.  If set to "stdout"
58                               then the debug will go there.  Also, you
59                               can specify a filehandle that already
60                               exists and use that.  debuglevel controls
61                               the amount of debug.  0 is none, 1 is
62                               normal, 2 is all.
63
64    Generate() - returns a key in Digest SHA1 form based on the current
65                 time and the PID.
66
67    Create(cacheString) - generates a key and caches it with the key
68                          of cacheString.  Create returns the key.
69
70    Compare(cacheString, - compares the key stored in the cache under
71            keyString)     cacheString with the keyString.  Returns 1
72                           if they match, and 0 otherwise.
73
74=head1 AUTHOR
75
76By Ryan Eatmon in May of 2000 for http://jabber.org.
77
78=head1 COPYRIGHT
79
80This module is free software; you can redistribute it and/or modify
81it under the same terms as Perl itself.
82
83=cut
84
85require 5.003;
86use strict;
87use FileHandle;
88use vars qw($VERSION);
89
90$VERSION = "2.0";
91
92sub new
93{
94    srand( time() ^ ($$ + ($$ << 15)));
95
96    my $proto = shift;
97    my $self = { };
98
99    $self->{DEBUG} = Net::Jabber::Debug->new(usedefault=>1,
100                                             header=>"NJ::Key");
101
102    $self->{VERSION} = $VERSION;
103   
104    $self->{CACHE} = {};
105
106    if (eval "require Digest::SHA")
107    {
108        $self->{DIGEST} = 1;
109        Digest::SHA->import(qw(sha1 sha1_hex sha1_base64));
110    }
111    else
112    {
113        print "ERROR:  You cannot use Key.pm unless you have Digest::SHA installed.\n";
114        exit(0);
115    }
116
117    bless($self, $proto);
118    return $self;
119}
120
121
122###########################################################################
123#
124# Generate - returns a random string based on the PID and time and a
125#            random number.  Then it creates an SHA1 Digest of that
126#            string and returns it.
127#
128###########################################################################
129sub Generate
130{
131    my $self = shift;
132
133    my $string = $$.time.rand(1000000);
134    $string = Digest::SHA::sha1_hex($string);
135    $self->{DEBUG}->Log1("Generate: key($string)");
136    return $string;
137}
138
139
140##############################################################################
141#
142# Create - Creates a key and caches the id for comparison later.
143#
144##############################################################################
145sub Create
146{
147    my $self = shift;
148    my ($cacheString) = @_;
149
150    $self->{DEBUG}->Log1("Create: cacheString($cacheString)");
151    my $key = $self->Generate();
152    $self->{DEBUG}->Log1("Create: key($key)");
153    $self->{CACHE}->{$cacheString} = $key;
154    return $key;
155}
156
157
158##############################################################################
159#
160# Compare - Compares the key with the key in the cache.
161#
162##############################################################################
163sub Compare
164{
165    my $self = shift;
166    my ($cacheString,$key) = @_;
167
168    $self->{DEBUG}->Log1("Compare: cacheString($cacheString) key($key)");
169    my $cacheKey = delete($self->{CACHE}->{$cacheString});
170    $self->{DEBUG}->Log1("Compare: cacheKey($cacheKey)");
171    return ($key eq $cacheKey);
172}
173
174
175
1761;
Note: See TracBrowser for help on using the repository browser.