]> git.zerfleddert.de Git - fhem-stuff/blame - HMLAN/hmlan-crypt-test.pl
rx/tx switching now handled in rf_asksin
[fhem-stuff] / HMLAN / hmlan-crypt-test.pl
CommitLineData
7836eda2
MG
1#!/usr/bin/perl -w
2
5dac16e0
MG
3# key can either be 32 HEX chars (HMLAN) or a password (wired gateway)
4#my $key="00112233445566778899AABBCCDDEEFF";
5#my $key="pA$$w0rd1!";
6
7my $key="00112233445566778899AABBCCDDEEFF";
8
9# ip address
7836eda2
MG
10my $hmlan="192.168.1.2";
11
12BEGIN {
13 use File::Basename;
14 push @INC, dirname($0);
15}
16
17use HMLAN_CRYPT;
18use IO::Socket::INET;
19use IO::Select;
5dac16e0 20use Digest::MD5;
7836eda2
MG
21
22#This should be random...
23my $my_iv = '00112233445566778899AABBCCDDEEFF';
24my $hmlan_iv = '';
25
26my $sock = IO::Socket::INET->new(
27 PeerAddr => $hmlan,
28 PeerPort => 1000,
29 Proto => 'tcp',
30 Blocking => 0);
31
32my $read_set = new IO::Select();
33$read_set->add($sock);
34
35$read_set->can_read();
36
37chomp(my $iv = <$sock>);
5dac16e0
MG
38if ($iv =~ /^H/) {
39 print "${iv}\n";
40 chomp($iv = <$sock>);
41}
42
7836eda2
MG
43if ($iv =~ /^V(................................)\r$/) {
44 $hmlan_iv = $1;
5dac16e0
MG
45 print $sock "V${my_iv}\r\n";
46} elsif ($iv =~ /^V(..),(................................)\r$/) {
47 $hmlan_iv = $2;
48 print $sock "V01,${my_iv}\r\n";
7836eda2
MG
49} else {
50 print STDERR "Unknown IV received: ${iv}\n";
51 exit(1);
52}
53
5dac16e0
MG
54if (length($key) < 32) {
55 $key = Digest::MD5::md5($key);
56} else {
57 $key = pack("H*", $key);
58}
7836eda2 59
5dac16e0
MG
60my $cipher_hmlan=HMLAN_CRYPT->new($key, pack("H*", $my_iv));
61my $cipher_pc=HMLAN_CRYPT->new($key, pack("H*", $hmlan_iv));
7836eda2
MG
62
63$read_set->add(\*STDIN);
64
65while(1) {
66 my @ready = $read_set->can_read();
67 foreach my $r (@ready) {
68 if ($r == $sock) {
69 while(my $buf=<$r>) {
70 print $cipher_hmlan->decrypt($buf);
71 }
72 } else {
73 my $buf = <$r>;
74 exit(1) if (!$buf);
75
76 print $sock $cipher_pc->encrypt($buf);
77 }
78 }
79}
Impressum, Datenschutz