/* This program shows encoding of an integer into a byte array and again decoding the byte array into integer. This encoding and decoding is basically used when we need to give a byte array to SHA256 or SHA512 */ #include <iostream> using std::cout; using std::endl; using std::cin; #include <cryptopp/integer.h> using CryptoPP::Integer; #include <cryptopp/sha.h> using CryptoPP::SHA512; Integer hashOfInteger(const Integer &a) // {0,1}* -> Zp* { cout<<"Finding hash of "<<a<<endl; // using SHA512 here int byteCount=a.BitCount()/8; if(a.BitCount()%8!=0) byteCount++; byte byteArray[byteCount]; byte digest[SHA512::DIGESTSIZE]; a.Encode(byteArray,byteCount); // now byte array contains bytes corresponding to the Integer a SHA512().CalculateDigest(digest,byteArray,byteCount); Integer result; result.Decode(digest,SHA512::DIGESTSIZE); return result; } int main() { int n; cin>>n; cout<<"result = "<<hashOfInteger(Integer::Power2(n))<<endl; return 0; }
Tuesday, 20 June 2017
Hash Functions and Encoding and Decoding of Integer in Crypto++
Scalar Multiplication in Elliptic curve in Crypto++
#include<bits/stdc++.h> using std::cout; using std::endl; #include <cryptopp/ecp.h> #include <cryptopp/asn.h> #include <cryptopp/integer.h> using CryptoPP::Integer; #include <cryptopp/eccrypto.h> using CryptoPP::ECP; // Prime field using CryptoPP::ECPPoint; using CryptoPP::DL_GroupParameters_EC; #include <cryptopp/asn.h> #include <cryptopp/oids.h> namespace ASN1 = CryptoPP::ASN1; int main() { DL_GroupParameters_EC<ECP> groupParameter = ASN1::secp160r1(); cout << "Modulus: "<< groupParameter.GetCurve().GetField().GetModulus() << endl; cout << "Cofactor: "<< groupParameter.GetCofactor() << endl; cout << "Coefficients" << endl; cout << " A: "<< groupParameter.GetCurve().GetA() << endl; cout << " B: "<< groupParameter.GetCurve().GetB() << endl; ECP curve=groupParameter.GetCurve(); Integer y = groupParameter.GetSubgroupGenerator().x; Integer x = groupParameter.GetSubgroupGenerator().y; ECPPoint P=ECPPoint(x,y); cout<<"P = "<<P.x<<" , "<<P.y<<endl; ECPPoint R = curve.ScalarMultiply(P,Integer(2)); cout<<"R = "<<R.x<<" , "<<R.y<<endl; return 0; }
Basic Elliptic Curve Parameters in Crypto++
#include<bits/stdc++.h> using std::cout; using std::endl; #include <cryptopp/ecp.h> #include <cryptopp/asn.h> #include <cryptopp/integer.h> using CryptoPP::Integer; #include <cryptopp/eccrypto.h> using CryptoPP::ECP; // Prime field using CryptoPP::ECPPoint; using CryptoPP::DL_GroupParameters_EC; #include <cryptopp/asn.h> #include <cryptopp/oids.h> namespace ASN1 = CryptoPP::ASN1; int main() { DL_GroupParameters_EC<ECP> groupParameter = ASN1::secp160r1(); cout << "Modulus: "<< groupParameter.GetCurve().GetField().GetModulus() << endl; cout << "Cofactor: "<< groupParameter.GetCofactor() << endl; cout << "Coefficients" << endl; cout << " A: "<< groupParameter.GetCurve().GetA() << endl; cout << " B: "<< groupParameter.GetCurve().GetB() << endl; ECP curve=groupParameter.GetCurve(); Integer y = groupParameter.GetSubgroupGenerator().x; Integer x = groupParameter.GetSubgroupGenerator().y; ECPPoint P=ECPPoint(x,y); cout<<"P = "<<P.x<<" , "<<P.y<<endl; return 0; }
Random number Generation in Crypto++
#include<iostream> using std::cout; using std::endl; #include<cryptopp/integer.h> using CryptoPP::Integer; #include<cryptopp/osrng.h> using CryptoPP::AutoSeededRandomPool; int main() { // Illustration of generating a random number using AutoSeededRandomPool AutoSeededRandomPool rng; cout<<Integer(rng,Integer(400),Integer(500))<<endl; return 0; }
Basics about Integer class in Crypto++
// refer this documentation - https://www.cryptopp.com/docs/ref/class_integer.html // http://www.cryptopp.com/docs/ref561/class_integer.html #include<iostream> #include<cryptopp/integer.h> using CryptoPP::Integer; using std::cout; using std::endl; int main() { // example showing calculation of 2^x for really big x cout<<"2**30000 = "<<Integer::Power2(30000)<<endl; // Power2 is a static member function of class. // addition, subtraction, multiplication, division of two big numbers Integer i1=Integer::Power2(100); Integer i2=Integer::Power2(50); cout<<"2**100 + 2**50 = "<<i1+i2<<endl; cout<<"2**100 - 2**50 = "<<i1-i2<<endl; cout<<"2**100 * 2**50 = "<<i1*i2<<endl; cout<<"2**100 / 2**50 = "<<i1/i2<<endl; // example showing use of modular operator Integer i3=Integer::Power2(1024); Integer i4(501); cout<<"2**1024 % 501 = "<<i3%i4<<endl; // example showing calculation of modular inverse Integer i5(3628800); Integer i6(2431); cout<<"modular inverse of 3628800 wrt 2431 = "<<i5.InverseMod(i6)<<endl; // example showing GCD of two numbers cout<<"GCD("<<i5<<","<<i6<<") = "<<Integer::Gcd(i5,i6)<<endl; Integer a,b,c; a=Integer(10241024); b=Integer(10241024); c=Integer(1000000007); // modular Multiplication cout<<a_times_b_mod_c(a,b,c)<<endl; // modular Exponentiation cout<<a_exp_b_mod_c(a,b,c)<<endl; return 0; }
Integer XOR in Crypto++
#include<iostream> #include<algorithm> using std::cout; using std::cin; using std::endl; #include<cryptopp/integer.h> using CryptoPP::Integer; Integer XOR(Integer &a,Integer &b) { Integer result=0; if(a>b) { for(int i=a.BitCount()-1;i>=b.BitCount();i--) result=result*2 + a.GetBit(i); for(int i=b.BitCount()-1;i>=0;i--) result=result*2 + (a.GetBit(i) ^ b.GetBit(i)); } else { for(int i=b.BitCount()-1;i>=a.BitCount();i--) result=result*2 + b.GetBit(i); for(int i=a.BitCount()-1;i>=0;i--) result=result*2 + (a.GetBit(i) ^ b.GetBit(i)); } return result; } void printBits(Integer &a) { for(int i=a.BitCount()-1;i>=0;i--) cout<<a.GetBit(i); cout<<endl; } int main() { int x,y; cin>>x>>y; Integer a(Integer::Power2(x)); Integer b(Integer::Power2(y)); cout<<XOR(a,b)<<endl; return 0; }
Printing the same character n number of times in Vim
Many times you need to type the same character many times and for that you need to long press that character and this wastes time. Well, there is a smarter way to do this in Vim.
Suppose you need to type _(underscore) 50 times.
So to do this, go in command mode by pressing Esc key and type the following:
50i_<Esc>
The moment you press <Esc>, the one undersocre that appears on the screen will be repeated 50 times, and now you can see 50 underscores on the screen one after the other.
Suppose you need to type _(underscore) 50 times.
So to do this, go in command mode by pressing Esc key and type the following:
50i_<Esc>
The moment you press <Esc>, the one undersocre that appears on the screen will be repeated 50 times, and now you can see 50 underscores on the screen one after the other.
Running python code from Vim and replacing the code with output
It is possible to write python code in vim and then execute the code. The output of the code will be placed in place of the python code. Let me illustrate you an example so that it becomes more clear.
Example:
Suppose that you have a string "Lavish" and you want to put each character of this string into a new line. We can write python code to do this.
for i in "Lavish":
print i
Now select the above two lines by going in visal mode and then execute the following commmand:
:'<,'>!python
This will replace the python code with following lines:
L
a
v
i
s
h
Now, you may not see the real difference when the string is small, but as the string goes bigger, the power of vim becomes more visible.
Similarly, as an another example, say you want to print the following thing in a text file
1 Lavish
2 Lavish
3 Lavish
4 Lavish
5 Lavish
I'm sure, you must have figured it out that the python code for this will be:
for i in range(1,6):
print str(i)+" Lavish"
and then follow the same thing again as described above. Similarly to the above mentioned examples, there is a lot of cool stuff that you can try out and make yourself more productive and efficient.
I hope this was interesting and you learned something from this. :)
Example:
Suppose that you have a string "Lavish" and you want to put each character of this string into a new line. We can write python code to do this.
for i in "Lavish":
print i
Now select the above two lines by going in visal mode and then execute the following commmand:
:'<,'>!python
This will replace the python code with following lines:
L
a
v
i
s
h
Now, you may not see the real difference when the string is small, but as the string goes bigger, the power of vim becomes more visible.
Similarly, as an another example, say you want to print the following thing in a text file
1 Lavish
2 Lavish
3 Lavish
4 Lavish
5 Lavish
I'm sure, you must have figured it out that the python code for this will be:
for i in range(1,6):
print str(i)+" Lavish"
and then follow the same thing again as described above. Similarly to the above mentioned examples, there is a lot of cool stuff that you can try out and make yourself more productive and efficient.
I hope this was interesting and you learned something from this. :)
Automatic Indentation of Code in Vim
Indentation is very important as it increases the readability of the codea and is considered to be a good programming practice. But a lot of times you may have to get some code that is not indented, and it is really irritating to mannualy indent each line. Vim provides a very eays way to indent your code just with unbelievably few keystrokies.
gg=G
is will indent your code from beginning of the file till the end of the file. Note that here gg means go to the starting of the file and = means to indent and G is for the end of the file. Puting it together we can read it as "indent the code from first line till the last line."
Similarly, to order to indent the current line, just place your cursor anywhere in that line and press
==
This will indent the current line.
gg=G
is will indent your code from beginning of the file till the end of the file. Note that here gg means go to the starting of the file and = means to indent and G is for the end of the file. Puting it together we can read it as "indent the code from first line till the last line."
Similarly, to order to indent the current line, just place your cursor anywhere in that line and press
==
This will indent the current line.
Moving to the matching paranthesis in Vim
A lot of times while programming, we need to move to the matching paranthesis/braces. Place your cursor under one of the paranthesis in normal mode and press % the cursor will move to the corresponding matching paranthesis, bracket or brace what so ever it is. This is an efficient way of moving around functions in programming languages.
Saving and Quiting in Vim (shortcut Method)
Normal way to save and quit a file in vim is using :wq in vim. This is used by most of the user, but this involves three different keystrokes. But there is another way to do this saving and quiting. Just press ZZ in the normal mode and the file will be written and then vim will exit. I personally think that this is more efficient because it involves only one keystroke and it does all the things. Hope you also find this way of saving and quiting easier.
Subscribe to:
Posts (Atom)