diff options
author | lloyd <[email protected]> | 2010-09-03 13:23:45 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2010-09-03 13:23:45 +0000 |
commit | cb9a034650620e4d392a2d2f978b687078e5002c (patch) | |
tree | d80914034f7130bc479d3c1bb2ea9f13619a5dd5 /src/math/bigint/big_code.cpp | |
parent | e201c88a7931bc892c7d608ee9130ceb7c0f2fef (diff) |
Update some callers that were using Hex_Encoder or Hex_Decoder but
really didn't need to.
The ones in symkey and big_code were actually calling accessor
functions to do the encoding themselves without a Pipe (should have
definitely recognized that as a code smell). These versions have
changed semantically with this checkin - previously they would completely
ignore bad inputs, but now invalid inputs are rejected. For instance, you
cannot say
SymmetricKey key("Only some of this is hex, most of it isn't");
And expect to get a valid key formed by filtering out the non-hex
characters and then decoding it. This is almost certainly a good thing.
Also fix include in Botan.xs
Diffstat (limited to 'src/math/bigint/big_code.cpp')
-rw-r--r-- | src/math/bigint/big_code.cpp | 32 |
1 files changed, 17 insertions, 15 deletions
diff --git a/src/math/bigint/big_code.cpp b/src/math/bigint/big_code.cpp index 74701e532..a8272390d 100644 --- a/src/math/bigint/big_code.cpp +++ b/src/math/bigint/big_code.cpp @@ -1,6 +1,6 @@ /* * BigInt Encoding/Decoding -* (C) 1999-2007 Jack Lloyd +* (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ @@ -23,8 +23,9 @@ void BigInt::encode(byte output[], const BigInt& n, Base base) { SecureVector<byte> binary(n.encoded_size(Binary)); n.binary_encode(binary); - for(u32bit j = 0; j != binary.size(); ++j) - Hex_Encoder::encode(binary[j], output + 2*j); + + hex_encode(reinterpret_cast<char*>(output), + &binary[0], binary.size()); } else if(base == Octal) { @@ -103,22 +104,23 @@ BigInt BigInt::decode(const byte buf[], u32bit length, Base base) r.binary_decode(buf, length); else if(base == Hexadecimal) { - SecureVector<byte> hex; - for(u32bit j = 0; j != length; ++j) - if(Hex_Decoder::is_valid(buf[j])) - hex.append(buf[j]); - - u32bit offset = (hex.size() % 2); - SecureVector<byte> binary(hex.size() / 2 + offset); + SecureVector<byte> binary; - if(offset) + if(length % 2) { - byte temp[2] = { '0', hex[0] }; - binary[0] = Hex_Decoder::decode(temp); + // Handle lack of leading 0 + const char buf0_with_leading_0[2] = { '0', buf[0] }; + binary = hex_decode(buf0_with_leading_0, 2); + + binary.append(hex_decode(reinterpret_cast<const char*>(&buf[1]), + length - 1, + false)); + } + else + binary = hex_decode(reinterpret_cast<const char*>(buf), + length, false); - for(u32bit j = offset; j != binary.size(); ++j) - binary[j] = Hex_Decoder::decode(hex+2*j-offset); r.binary_decode(binary, binary.size()); } else if(base == Decimal || base == Octal) |