signature = Base64.encode64(@stored_priv_key.dsa_sign_asn1(message))
puts “first signature length is #{signature.length}”
=> first signature length is 78
then later in a different method I have
signature = decrypted[0,77]
puts “second signature length is #{signature.length}”
=> signature length is 77
Now I can get this to work (and it validates the signature if it is
correct) by making the range [0,78] (which returns a
78 character string, which is the signature, considering it validates)
but it seems like it is not starting counting 0 towards the length,
which is not the expected behavior. Even though it works when I make the
range 0,78 it is not how it is supposed to be as far as I can tell,
because it should be starting at 0 which counts for length 1 and then
going up to position 77 which counts for 77 other additions to length in
addition to the first 0.
I don’t have ALL of my code here and maybe you need to see it all, but
what could ever cause the behavior or an indexed range creating a length
that is as large as the largest number in the range (instead of +1,
taking the 0 position into account)
Also when I index the rest of the message I need to do it like this:
puts “message is #{decrypted[78,decrypted.length]}”
notice that I am starting from 78! (which is where I need to end
getting the signature from for it to work…
signature = Base64.decode64(decrypted[0,78])
)
This makes no sense to me, even though it works and returns my validated
message. It is doing exactly what I want it to do right now, but it
isn’t coded in a way that I think should work, and when I code it in the
way that I think should work it is not working.
I only got to working code by printing signature/message etc and
adjusting the range until it started to work, so trial and error. When I
did it what I think is the correct way the first time, it was giving me
errors and debugging it has got me to this point, where it works
perfectly but the code just does not seem correct to me…what could
possibly be causing this?!
sometimes returns a signature of 74 characters and other times 78. This
of course leads to crashing, when the signature size is 74. It seems to
happen regardless of the input, as you can see here “message” is fixed
size. I have already checked to make sure @stored_priv_key does not
change. It appears to be random if the signature returned is 74 or 78
characters, all other variables and input are the same but after enough
iterations it always returns a signature that is 74 characters
eventually (sometimes on the first run, usually after no more than
five).
More confusing behavior and a different problem but I figure I can
put it in the same thread to avoid clutter.
If I don’t base64 encode it , it returns either 54, 55 or 56 bytes also
with all other variables being equal.
signature = decrypted[0,77]
puts “second signature length is #{signature.length}”
=> signature length is 77
[…]
but it seems like it is not starting counting 0 towards the length,
which is not the expected behavior. Even though it works when I make the
range 0,78 it is not how it is supposed to be as far as I can tell,
because it should be starting at 0 which counts for length 1 and then
going up to position 77 which counts for 77 other additions to length in
addition to the first 0.
No, that’s a misunderstanding. When you write str[m, n], you get the
substring with the length n starting at character m. It does not
return the substring between the characters m and n.
For example:
str = “abcdefghijklmnopqrstuvwxyz”
get the substring with length 3 starting at character 6 (“g”)
puts str[6, 3] # outputs “ghi”
This behaviour is shared by other languages like PHP or JavaScript.
If you want the substring between characters, you have to supply a range
rather than two integers:
get the substring between character 2 (“c”) and 6 (“g”)
str[3…6] # outputs “cdefg”
By the way: If you are not sure about how a method works, you should
first check the documentation. This will save you a lot of time and
effort.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.