diff --git a/lib/we_whisper/cipher.rb b/lib/we_whisper/cipher.rb index 3bc1605..b5fdd45 100644 --- a/lib/we_whisper/cipher.rb +++ b/lib/we_whisper/cipher.rb @@ -35,7 +35,6 @@ module WeWhisper decode_padding(plain) end - # app_id or corp_id def pack(content, app_id) random = SecureRandom.hex(8) text = content.force_encoding('ASCII-8BIT') @@ -65,7 +64,7 @@ module WeWhisper def decode_padding(plain) pad = plain.bytes[-1] - # no padding + # if padding is less than 1 or larger than block size, then set to 0 pad = 0 if pad < 1 || pad > BLOCK_SIZE plain[0...(plain.length - pad)] end diff --git a/lib/we_whisper/signature.rb b/lib/we_whisper/signature.rb index a84d0dd..a065c80 100644 --- a/lib/we_whisper/signature.rb +++ b/lib/we_whisper/signature.rb @@ -2,9 +2,9 @@ require 'digest/sha2' module WeWhisper module Signature - def self.hexdigest(token, timestamp, nonce, msg_encrypt) + def self.sign(token, timestamp, nonce, encrypted) array = [token, timestamp, nonce] - array << msg_encrypt unless msg_encrypt.nil? + array << encrypted unless encrypted.nil? Digest::SHA1.hexdigest array.compact.collect(&:to_s).sort.join end end diff --git a/lib/we_whisper/whisper.rb b/lib/we_whisper/whisper.rb index b5212b9..71773e7 100644 --- a/lib/we_whisper/whisper.rb +++ b/lib/we_whisper/whisper.rb @@ -35,7 +35,7 @@ module WeWhisper # 2. If we need to validate signature, generate one from the encrypted text # and check with the Signature in message if options[:assert_signature] && signature = Message.get_signature_from_messge(message) - sign = Signature.hexdigest(token, timestamp, nonce, encrypted_text) + sign = Signature.sign(token, timestamp, nonce, encrypted_text) raise InvalidSignature if sign != signature end @@ -55,7 +55,7 @@ module WeWhisper encrypt = Base64.strict_encode64(encrypt(pack(message, appid), encoding_aes_key)) # 2. Create signature - sign = Signature.hexdigest(token, timestamp, nonce, encrypt) + sign = Signature.sign(token, timestamp, nonce, encrypt) # 3. Construct xml Message.to_xml(encrypt, sign, timestamp, nonce) diff --git a/spec/we_whisper/signature_spec.rb b/spec/we_whisper/signature_spec.rb new file mode 100644 index 0000000..5e471fd --- /dev/null +++ b/spec/we_whisper/signature_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +describe WeWhisper::Signature do + + let(:timestamp) { "1415979516" } + let(:nonce) { "1320562132" } + let(:signature) { "096d8cda45e4678ca23460f6b8cd281b3faf1fc3" } + let(:token) { "spamtest" } + let(:encrypted) { "3kKZ++U5ocvIF8dAHPct7xvUqEv6vplhuzA8Vwj7OnVcBu9fdmbbI41zclSfKqP6/bdYAxuE3x8jse43ImHaV07siJF473TsXhl8Yt8task0n9KC7BDA73mFTwlhYvuCIFnU6wFlzOkHyM5Bh2qpOHYk5nSMRyUG4BwmXpxq8TvLgJV1jj2DXdGW4qdknGLfJgDH5sCPJeBzNC8j8KtrJFxmG7qIwKHn3H5sqBf6UqhXFdbLuTWL3jwE7yMLhzOmiHi/MX/ZsVQ7sMuBiV6bW0wkgielESC3yNUPo4q/RMAFEH0fRLr76BR5Ct0nUbf9PdClc0RdlYcztyOs54X/KLbYRNCQ2kXxmJYL6ekdNe70PCAReIEfXEp+pGpry4ss8bD6LKAtNvBJUwHshZe6sbf+fOiDiuKEqp1wdQLmgN+8nX62LklySWr8QrNCpsmKClxco0kbVYNX/QVh5yd0UA1sAqIn6baZ9G+Z/OXG+Q4n9lUuzLprLhDBPaCvXm4N14oqXNcw7tqU2xfhYNIDaD72djyIc/4eyAi2ZsJ+3hb+jgiISR5WVveRWYYqGZGTW3u+27JiXEo0fs3DQDbGVIcYxaMgU/RRIDdXzZSFcf6Z1azjzCDyV9FFEsicghHn" } + + it "signs message" do + expect(subject.sign(token, timestamp, nonce, encrypted)).to eq signature + end + +end -- libgit2 0.21.0