Permalink
Browse files

Added sign-only support

  • Loading branch information...
drbrain committed Mar 30, 2012
1 parent 393b408 commit 11abf19e32d2f3da1eb109669b30ab559538643d
Showing with 88 additions and 6 deletions.
  1. +7 −6 lib/ruby-growl/gntp.rb
  2. +81 −0 test/test_growl_gntp.rb
View
@@ -126,19 +126,20 @@ def packet type, headers
body << nil
body = body.join "\r\n"
+ if @password then
+ digest = Digest::SHA512
+ key, hash, salt = key_hash digest
+ key_info = "SHA512:#{hash}.#{Digest.hexencode salt}"
+ end
+
if @encrypt == 'NONE' then
- packet << "GNTP/1.0 #{type} NONE"
+ packet << ["GNTP/1.0", type, "NONE", key_info].compact.join(' ')
packet << body
else
- digest = Digest::SHA512
-
- key, hash, salt = key_hash digest
encipher, iv = cipher key
encrypt_info = "#{@encrypt}:#{Digest.hexencode iv}"
- key_info = "SHA512:#{hash}.#{Digest.hexencode salt}"
-
packet << "GNTP/1.0 #{type} #{encrypt_info} #{key_info}"
encrypted = encipher.update(body)
View
@@ -314,6 +314,49 @@ def test_packet_encrypt_des
assert_equal expected, decrypted
end
+ def test_packet_encrypt_3des
+ @gntp.encrypt = '3DES'
+ @gntp.password = 'password'
+
+ packet = @gntp.packet 'REGISTER', ["Foo: bar"]
+
+ info, body = packet.split "\r\n", 2
+
+ _, _, algorithm_info, key_info = info.split ' '
+
+ cipher, iv = algorithm_info.split ':'
+
+ assert_equal '3DES', cipher
+
+ iv = [iv].pack 'H*'
+
+ cipher = OpenSSL::Cipher.new Growl::GNTP::ENCRYPTION_ALGORITHMS[cipher]
+
+ assert_equal 'DES-EDE3-CBC', cipher.name
+
+ _, salt = key_info.split '.', 2
+
+ salt = [salt].pack 'H*'
+
+ key = Digest::SHA512.digest "password#{salt}"
+
+ body = body.chomp "\r\n\r\n"
+
+ decrypted = decrypt cipher, key, iv, body
+
+ expected = <<-EXPECTED
+Application-Name: test-app\r
+Origin-Software-Name: ruby-growl\r
+Origin-Software-Version: 3.0\r
+Origin-Platform-Name: ruby\r
+Origin-Platform-Version: 2.0.0\r
+Connection: close\r
+Foo: bar\r
+ EXPECTED
+
+ assert_equal expected, decrypted
+ end
+
def test_packet_encrypt_aes
@gntp.encrypt = 'AES'
@gntp.password = 'password'
@@ -357,6 +400,44 @@ def test_packet_encrypt_aes
assert_equal expected, decrypted
end
+ def test_packet_hash
+ @gntp.password = 'password'
+
+ packet = @gntp.packet 'REGISTER', ["Foo: bar"]
+
+ info, body = packet.split "\r\n", 2
+
+ _, _, algorithm_info, key_info = info.split ' '
+
+ assert_equal 'NONE', algorithm_info
+
+ key_info =~ /:(.*)\./
+
+ digest = $`
+ key_hash = $1
+ salt = $'
+
+ salt = [salt].pack 'H*'
+
+ expected_key = Digest::SHA512.digest "password#{salt}"
+ expected_key_hash = Digest::SHA512.hexdigest expected_key
+
+ assert_equal expected_key_hash, key_hash
+
+ expected = <<-EXPECTED
+Application-Name: test-app\r
+Origin-Software-Name: ruby-growl\r
+Origin-Software-Version: 3.0\r
+Origin-Platform-Name: ruby\r
+Origin-Platform-Version: 2.0.0\r
+Connection: close\r
+Foo: bar\r
+\r
+\r
+ EXPECTED
+
+ assert_equal expected, body
+ end
def test_packet_notify
expected = <<-EXPECTED
GNTP/1.0 NOTIFY NONE\r

0 comments on commit 11abf19

Please sign in to comment.