forked from HikariObfuscator/Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CryptoUtils.cpp
65 lines (62 loc) · 1.75 KB
/
CryptoUtils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// For open-source license, please refer to [License](https://github.com/HikariObfuscator/Hikari/wiki/License).
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/Obfuscation/CryptoUtils.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/Twine.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Format.h"
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <string>
#include <chrono>
using namespace llvm;
namespace llvm {
ManagedStatic<CryptoUtils> cryptoutils;
}
CryptoUtils::CryptoUtils() {
}
uint32_t CryptoUtils::scramble32(uint32_t in,std::map<uint32_t/*IDX*/,uint32_t/*VAL*/>& VMap) {
if(VMap.find(in)==VMap.end()){
uint32_t V=get_uint32_t();
VMap[in]=V;
return V;
}
else{
return VMap[in];
}
}
CryptoUtils::~CryptoUtils() {
if(eng!=nullptr){
delete eng;
}
}
void CryptoUtils::prng_seed(){
using namespace std::chrono;
std::uint_fast64_t ms = duration_cast< milliseconds >(system_clock::now().time_since_epoch()).count();
errs()<<format("std::mt19937_64 seeded with current timestamp: %" PRIu64"",ms)<<"\n";
eng=new std::mt19937_64(ms);
}
void CryptoUtils::prng_seed(std::uint_fast64_t seed){
errs()<<format("std::mt19937_64 seeded with: %" PRIu64"",seed)<<"\n";
eng=new std::mt19937_64(seed);
}
std::uint_fast64_t CryptoUtils::get_raw(){
if(eng==nullptr){
prng_seed();
}
return (*eng)();
}
uint32_t CryptoUtils::get_range(uint32_t min,uint32_t max) {
if(max==0){
return 0;
}
std::uniform_int_distribution<uint32_t> dis(min, max-1);
return dis(*eng);
}