forked from TUBITAK-TUTEL/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobfuscator.h
101 lines (80 loc) · 3.74 KB
/
obfuscator.h
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2017-2020 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef VERIBLE_COMMON_STRINGS_OBFUSCATOR_H_
#define VERIBLE_COMMON_STRINGS_OBFUSCATOR_H_
#include <functional>
#include <string>
#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "common/strings/compare.h"
#include "common/strings/random.h"
#include "common/util/bijective_map.h"
namespace verible {
// Obfuscator translates input strings into randomly generated strings
// in a manner that tracks which inputs it has seen before, creating a mapping
// and re-using it as more unique input words are seen. The obfuscation is
// intended to be reversible, so that a one-to-one mapping between original and
// obfuscated text is maintained.
//
// By default, Obfuscator operates in encoding mode, described above.
// In decoding mode (set by set_decode_mode()), the obfuscated strings are
// used to lookup their original counterparts, and unfamiliar strings are kept
// intact (never added to the internal map).
//
// The save() and load() functions can be used to re-apply previously used
// substitutions written/read from a text file.
class Obfuscator {
public:
typedef std::function<std::string(absl::string_view)> generator_type;
typedef BijectiveMap<std::string, std::string, StringViewCompare,
StringViewCompare>
translator_type;
explicit Obfuscator(generator_type g) : generator_(g), translator_() {}
// Declares a mapping from key-string to value-string that will be
// used in obfuscation. This is useful for applying previously used
// translations. Returns true if key-value pair was successfully inserted,
// else returns false if either key or value were already mapped.
bool encode(absl::string_view key, absl::string_view value);
void set_decode_mode(bool decode) { decode_mode = decode; }
bool is_decoding() const { return decode_mode; }
// Obfuscates input string with a replacement, and records the substitution
// for later re-use. Returns the replacement string.
absl::string_view operator()(absl::string_view input);
// Read-only view of string translation map.
const translator_type& GetTranslator() const { return translator_; }
// Parses a mapping dictionary, and pre-loads the translator map with it.
// Format: one entry per line, each line is space-separated pair of
// identifiers.
absl::Status load(absl::string_view);
// Returns a string representation of the internal identifier map.
// See format description for ::load().
std::string save() const;
private:
// Generates a random substitution string, for obfuscation.
generator_type generator_;
// Keeps track of transformations done on seen strings.
translator_type translator_;
// If true, apply reverse translation of identifiers, and do not generate any
// new obfuscation mappings.
bool decode_mode = false;
};
class IdentifierObfuscator : public Obfuscator {
typedef Obfuscator parent_type;
public:
IdentifierObfuscator() : Obfuscator(RandomEqualLengthIdentifier) {}
// Same as inherited method, but verifies that key and value are equal length.
bool encode(absl::string_view key, absl::string_view value);
};
} // namespace verible
#endif // VERIBLE_COMMON_STRINGS_OBFUSCATOR_H_