Guest User

map uint64_t to random uint64_t

a guest
Aug 6th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. // map every uint64_t to a (seemingly) random other uint64_t uniquely
  2. //
  3. // the mapping is done by encrypting the uint64_t as a single blowfish block
  4. //
  5. // CC0 copyright
  6. // https://creativecommons.org/publicdomain/zero/1.0/
  7.  
  8. #include <iostream>
  9.  
  10. #include <iomanip>
  11. #include <cstdint>
  12. #include <chrono>
  13.  
  14. #include <cryptopp/sha.h>
  15.  
  16. #include <cryptopp/modes.h>
  17. #include <cryptopp/blowfish.h>
  18. #include <cryptopp/filters.h>
  19. #include <cryptopp/hex.h>
  20.  
  21. using namespace std;
  22. using namespace std::chrono;
  23. using namespace CryptoPP;
  24.  
  25.  
  26. string to_hexstr(byte const* const block, size_t length){
  27.     string hex_str;
  28.     ArraySource( block, length, true,
  29.         new HexEncoder(
  30.             new StringSink( hex_str)
  31.         )
  32.     );
  33.     return hex_str;
  34. }
  35.  
  36. uint64_t to_random(
  37.     ECB_Mode<Blowfish>::Encryption& e,
  38.     uint64_t in
  39. ){
  40.             byte* plain_block = (byte*)&in;
  41.             byte cipher_block[Blowfish::BLOCKSIZE];
  42.  
  43.             // thing being timed
  44.             ArraySource(
  45.                 plain_block,
  46.                 Blowfish::BLOCKSIZE,
  47.                 true,
  48.                 new StreamTransformationFilter(
  49.                     e,
  50.                     new ArraySink(
  51.                         cipher_block,
  52.                         Blowfish::BLOCKSIZE
  53.                     )
  54.                 )
  55.             );
  56.             uint64_t result = *((uint64_t*)cipher_block);
  57.             return result;
  58. }
  59.  
  60. void f0(){
  61.     SecByteBlock key(Blowfish::DEFAULT_KEYLENGTH);
  62.     {
  63.         for(int i = 0; i<Blowfish::DEFAULT_KEYLENGTH; i++){
  64.             key[i] = byte(0);
  65.         }
  66.     }
  67.     ECB_Mode<Blowfish>::Encryption e;
  68.     e.SetKey(key, key.size());
  69.  
  70.     cout << "1 -> " << to_random(e, 1) << endl;
  71.  
  72. }
  73.  
  74. int main(int argc, char** argv){
  75.     f0();
  76.     //blowfish_benchmark();
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment