|
ATLAS Offline Software
|
Go to the documentation of this file.
43 asg::AsgMessaging(
name), m_n(0), m_e(0), m_d(0),
44 m_isOkForEnc(false), m_isOkForDec(false) {
47 srand(
static_cast<unsigned>(time(0)));
63 std::pair<std::string, std::string>
keys =
64 std::make_pair(
"__NO_PRIV_KEY__",
"__NO_PUB_KEY__");
156 <<
a << Form(
" (%a) !",
a));
169 if (
a > 0. || std::isnan(
a) ) {
177 <<
a << Form(
" (%a) !",
a));
200 while (
p ==
q || m_e < 2 || m_e >=
phi ||
m_d < 2
203 while (
p ==
q || dlog2 < 0.1 || dlog2 > 30. ) {
206 dlog2 = fabs(log2(
p)-log2(
q));
249 std::vector<LLI_t>
r;
255 r.push_back(
r[
i-2] %
r[
i-1]);
278 if ( ((
phi *
i + 1) %
e) == 0 ) {
290 unsigned int ra = (
unsigned int)(
log(
a)/
log(16.))+1;
298 TString tstr = Form(
"%02x%02x%02x%0*x%0*llx%0*x%0*llx",
303 return std::string(tstr.Data());
308 std::pair<SimpleEncrypter::ULLI_t, SimpleEncrypter::ULLI_t>
311 std::pair<ULLI_t, ULLI_t>
keys(0,0);
314 if (
str.IsHex() &&
str.Length() > 3) {
316 unsigned int ndigits = strtoul(TString(
str(0,2)).
Data(),
nullptr, 16);
317 unsigned int ra = strtoul(TString(
str(2,2)).
Data(),
nullptr, 16);
318 unsigned int rb = strtoul(TString(
str(4,2)).
Data(),
nullptr, 16);
319 if (
str.Length() == (
int)(2*ndigits + 6) ) {
320 keys.first = strtoll(TString(
str(ndigits+6-ra, ra)).
Data(),
325 ATH_MSG_ERROR(
"Private/public key must be a hex string of " <<
329 ATH_MSG_ERROR(
"Private/public key must be a hex string of " <<
346 if (
sizeof(
float) <=
sizeof(
ULLI_t) ) {
348 if (
sizeof(
float) ==
sizeof(
int) ) {
357 char* pval =
reinterpret_cast<char*
>(&
val);
359 for (
unsigned int i=0;
i<
sizeof(
float); ++
i) {
361 for (
unsigned int j=0; j<CHAR_BIT; ++j) {
362 unsigned int n =
i*CHAR_BIT + j;
363 unsigned int bit = (*(pval+
i) >> j) & 1;
364 if ( bit > 0 )
res |= 1 <<
n;
370 <<
sizeof(
float) <<
" > " <<
sizeof(
LLI_t));
384 unsigned int r = (
int)(std::log2(
val))+1;
387 if (
sizeof(
float)*CHAR_BIT >=
r ) {
389 if (
sizeof(
float) ==
sizeof(
int) ) {
398 char* pres =
reinterpret_cast<char*
>(&
res);
400 for (
unsigned int i=0;
i<
sizeof(
float); ++
i) {
402 for (
unsigned int j=0; j<CHAR_BIT; ++j) {
403 unsigned int n =
i*CHAR_BIT + j;
404 unsigned int bit = (
val >>
n) & 1;
405 if ( bit > 0 ) *(pres+
i) |= 1 << j;
411 <<
sizeof(
float)*CHAR_BIT <<
" < " <<
r);
430 }
while (
c > rmask );
449 }
while (
d > rmask );
504 ATH_MSG_ERROR(
"Setup not OK for encryption: public key set?");
520 ATH_MSG_ERROR(
"Setup not OK for decryption: private key set?");
ULLI_t decryptFPECycle(ULLI_t a) const
Decrypt using format preserving encryption w.r.t.
virtual ULLI_t genPrime() const
Find a prime number.
SimpleEncrypter(const std::string &name="SimpleEncrypter")
Main constructor.
virtual ULLI_t encrypt(ULLI_t x)
Encrypt a positive integer value.
virtual std::pair< ULLI_t, ULLI_t > decodeKeyString(std::string str) const
Decode hex string to two integers.
virtual ULLI_t genDecryptionExponent(ULLI_t phi, ULLI_t e) const
Find decryption exponent.
virtual void setPubKey(std::string keystr)
Set public key.
virtual ULLI_t floatBitsToInt(float val) const
virtual std::pair< std::string, std::string > genKeyPair()
Generate private and public keys.
Provide simple asymmetric encryption for blinding of float values.
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.
ULLI_t encryptFPECycle(ULLI_t a) const
virtual ULLI_t genCoprime(ULLI_t n) const
Find a coprime number.
ULLI_t powerMod(ULLI_t a, ULLI_t d, ULLI_t n) const
Exponentiate a with d observing modulus n.
bool isOkForEnc()
Check setup readiness for encryption.
bool m_isOkForEnc
indicates that keys are set and range checks are ok
virtual void setPrivKey(std::string keystr)
Set private key.
ULLI_t decryptInternal(ULLI_t x) const
Decrypt integer (internal)
virtual ULLI_t greatestCommonDenominator(ULLI_t n1, ULLI_t n2) const
Find greatest common denominator.
virtual ~SimpleEncrypter()
Default destructor.
virtual bool isPrime(ULLI_t n) const
Check for being a prime number.
static const unsigned int m_MAXHEXDIGITS
maximum number of hex digits for key parts
std::pair< std::vector< unsigned int >, bool > res
std::string str(const TrigT2MbtsBits_v1 &trigT2MbtsBits)
virtual void genKeyPairInternal()
static const ULLI_t m_MAXRANGE
unsigned long long int ULLI_t
virtual float intBitsToFloat(ULLI_t val) const
Interpret bits of integer as floating point number.
virtual std::string getPrivKey() const
Get private key.
virtual ULLI_t decrypt(ULLI_t x)
Decrypt a positive integer value.
ULLI_t encryptInternal(ULLI_t x) const
Encrypt integer (internal)
#define ATH_MSG_WARNING(x)
ULLI_t m_e
encryption exponent: public key part II
long long int LLI_t
Useful typedefs.
ULLI_t m_d
decryption exponent: private key part II
virtual std::string getPubKey() const
Get public key.
bool isOkForDec()
Check setup readiness for decryption.
virtual std::string keyToString(ULLI_t a, ULLI_t b) const
static const ULLI_t m_MINRANGE