diff options
author | tomsmeding <tom.smeding@gmail.com> | 2016-10-08 21:57:51 +0200 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2016-10-08 21:57:59 +0200 |
commit | 93b1a18fd61890eb4976a7f52d17e0990ccca661 (patch) | |
tree | 50cb66ad5dcc6c098bea1422a1237dfbefb7d65a | |
parent | 27472e604eeb74b3b60313d38d537c7e0e83151b (diff) |
Fix some serious bugs with casting
-rw-r--r-- | bigint.cpp | 24 | ||||
-rw-r--r-- | bigint.h | 4 | ||||
-rw-r--r-- | rsa.cpp | 4 |
3 files changed, 24 insertions, 8 deletions
@@ -16,8 +16,15 @@ Bigint Bigint::two(2); Bigint::Bigint() :sign(1){} +Bigint::Bigint(Bigint &&o) + :digits(move(o.digits)),sign(o.sign){ + o.normalise(); + checkconsistent(); +} + Bigint::Bigint(const string &repr){ stringstream(repr)>>*this; + checkconsistent(); } Bigint::Bigint(slongdigit_t v) @@ -145,6 +152,15 @@ void Bigint::checkconsistent(){ assert(digits.size()!=0||sign==1); } +Bigint& Bigint::operator=(Bigint &&o){ + sign=o.sign; + digits=move(o.digits); + o.normalise(); + normalise(); + checkconsistent(); + return *this; +} + Bigint& Bigint::operator=(slongdigit_t v){ digits.clear(); if(v==0){ @@ -498,21 +514,21 @@ string Bigint::serialiseMantissa() const { int sz=digits.size(); for(int i=0;i<sz;i++){ for(int j=0;j<(int)sizeof(digit_t);j++){ - s[i*sizeof(digit_t)+j]=(digits[i]>>(8*j))&255; + s[i*sizeof(digit_t)+j]=(digits[i]>>(8*j))&0xff; } } return s; } void Bigint::deserialiseMantissa(const string &s){ - assert(s.size()%4==0); + assert(s.size()%sizeof(digit_t)==0); sign=1; - int sz=s.size()/4; + int sz=s.size()/sizeof(digit_t); digits.resize(sz); for(int i=0;i<sz;i++){ digits[i]=0; for(int j=0;j<(int)sizeof(digit_t);j++){ - digits[i]|=s[i*sizeof(digit_t)+j]<<(8*j); + digits[i]|=(uint8_t)s[i*sizeof(digit_t)+j]<<(8*j); } } shrink(); @@ -31,7 +31,7 @@ private: public: Bigint(); Bigint(const Bigint&)=default; - Bigint(Bigint&&)=default; + Bigint(Bigint&&); explicit Bigint(const std::string&); explicit Bigint(sdigit_t); explicit Bigint(digit_t); @@ -39,7 +39,7 @@ public: explicit Bigint(longdigit_t); Bigint& operator=(const Bigint&)=default; - Bigint& operator=(Bigint&&)=default; + Bigint& operator=(Bigint&&); Bigint& operator=(slongdigit_t); Bigint& operator=(longdigit_t); Bigint& operator=(sdigit_t); @@ -51,8 +51,8 @@ namespace RSA{ PrivateKey importkey(const string &pub,const string &priv){ string pubdeser=Base64::decode(pub); assert(pubdeser.size()>4); - int modlen=(unsigned char)(pubdeser[0])+(unsigned char)(pubdeser[1]<<8)+ - (unsigned char)(pubdeser[2]<<16)+(unsigned char)(pubdeser[3]<<24); + int modlen=(uint8_t)pubdeser[0]+((uint8_t)pubdeser[1]<<8)+ + ((uint8_t)pubdeser[2]<<16)+((uint8_t)pubdeser[3]<<24); assert((int)pubdeser.size()-4>modlen); PrivateKey key; key.pub.mod.deserialiseMantissa(string(pubdeser.begin()+4,pubdeser.begin()+(4+modlen))); |