[0ff8d110] | 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 | |
---|
| 23 | package Net::Jabber::Key; |
---|
| 24 | |
---|
| 25 | =head1 NAME |
---|
| 26 | |
---|
| 27 | Net::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 | |
---|
[cb54527] | 43 | $Key = Net::Jabber::Key->new(); |
---|
[0ff8d110] | 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 | |
---|
| 76 | By Ryan Eatmon in May of 2000 for http://jabber.org. |
---|
| 77 | |
---|
| 78 | =head1 COPYRIGHT |
---|
| 79 | |
---|
| 80 | This module is free software; you can redistribute it and/or modify |
---|
| 81 | it under the same terms as Perl itself. |
---|
| 82 | |
---|
| 83 | =cut |
---|
| 84 | |
---|
| 85 | require 5.003; |
---|
| 86 | use strict; |
---|
| 87 | use FileHandle; |
---|
| 88 | use vars qw($VERSION); |
---|
| 89 | |
---|
| 90 | $VERSION = "2.0"; |
---|
| 91 | |
---|
| 92 | sub new |
---|
| 93 | { |
---|
| 94 | srand( time() ^ ($$ + ($$ << 15))); |
---|
| 95 | |
---|
| 96 | my $proto = shift; |
---|
| 97 | my $self = { }; |
---|
| 98 | |
---|
[cb54527] | 99 | $self->{DEBUG} = Net::Jabber::Debug->new(usedefault=>1, |
---|
| 100 | header=>"NJ::Key"); |
---|
[0ff8d110] | 101 | |
---|
| 102 | $self->{VERSION} = $VERSION; |
---|
| 103 | |
---|
| 104 | $self->{CACHE} = {}; |
---|
| 105 | |
---|
| 106 | if (eval "require Digest::SHA1") |
---|
| 107 | { |
---|
| 108 | $self->{DIGEST} = 1; |
---|
| 109 | Digest::SHA1->import(qw(sha1 sha1_hex sha1_base64)); |
---|
| 110 | } |
---|
| 111 | else |
---|
| 112 | { |
---|
| 113 | print "ERROR: You cannot use Key.pm unless you have Digest::SHA1 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 | ########################################################################### |
---|
| 129 | sub Generate |
---|
| 130 | { |
---|
| 131 | my $self = shift; |
---|
| 132 | |
---|
| 133 | my $string = $$.time.rand(1000000); |
---|
| 134 | $string = Digest::SHA1::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 | ############################################################################## |
---|
| 145 | sub 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 | ############################################################################## |
---|
| 163 | sub 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 | |
---|
| 176 | 1; |
---|