|
| 1 | +// Copyright 2025 Rina |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package util |
| 16 | + |
| 17 | +import ( |
| 18 | + "regexp" |
| 19 | + "strings" |
| 20 | + "unicode" |
| 21 | + |
| 22 | + "golang.org/x/exp/rand" |
| 23 | +) |
| 24 | + |
| 25 | +var leetMap = map[rune][]rune{ |
| 26 | + 'A': {'4', 'A', 'a'}, |
| 27 | + 'B': {'8', 'B', 'b'}, |
| 28 | + 'C': {'C', 'c'}, |
| 29 | + 'D': {'D', 'd'}, |
| 30 | + 'E': {'3', 'E', 'e'}, |
| 31 | + 'F': {'F', 'f'}, |
| 32 | + 'G': {'6', 'G', 'g'}, |
| 33 | + 'H': {'H', 'h'}, |
| 34 | + 'I': {'I', 'i', '1'}, |
| 35 | + 'J': {'J', 'j'}, |
| 36 | + 'K': {'K', 'k'}, |
| 37 | + 'L': {'L', 'l', '1'}, |
| 38 | + 'M': {'M', 'm'}, |
| 39 | + 'N': {'N', 'n'}, |
| 40 | + 'O': {'O', 'o', '0'}, |
| 41 | + 'P': {'P', 'p'}, |
| 42 | + 'Q': {'Q', 'q'}, |
| 43 | + 'R': {'R', 'r'}, |
| 44 | + 'S': {'S', 's', '5', '$'}, |
| 45 | + 'T': {'T', 't', '7'}, |
| 46 | + 'U': {'U', 'u'}, |
| 47 | + 'V': {'V', 'v'}, |
| 48 | + 'W': {'W', 'w'}, |
| 49 | + 'X': {'X', 'x'}, |
| 50 | + 'Y': {'Y', 'y'}, |
| 51 | + 'Z': {'Z', 'z', '2'}, |
| 52 | +} |
| 53 | + |
| 54 | +func Leetify(input string, seed uint64) string { |
| 55 | + rand.Seed(seed) |
| 56 | + |
| 57 | + var result strings.Builder |
| 58 | + for _, char := range input { |
| 59 | + if leetChars, ok := leetMap[unicode.ToUpper(char)]; ok { |
| 60 | + result.WriteRune(leetChars[rand.Intn(len(leetChars))]) |
| 61 | + } else { |
| 62 | + result.WriteRune(char) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return result.String() |
| 67 | +} |
| 68 | + |
| 69 | +func GenerateFlagContent(format string, seed string) string { |
| 70 | + // Use [] to wrap the string to be leetified |
| 71 | + // Use {hash} to insert the sha256ed seed |
| 72 | + // Use {uuid} to insert a random uuid |
| 73 | + |
| 74 | + leetRe := regexp.MustCompile(`\[([^\[\]]+)\]`) |
| 75 | + |
| 76 | + result := format |
| 77 | + result = strings.ReplaceAll(result, "{hash}", SHA256(seed)[:8]) |
| 78 | + result = strings.ReplaceAll(result, "{uuid}", UUID()) |
| 79 | + |
| 80 | + leet := leetRe.FindAllString(result, -1) |
| 81 | + for _, l := range leet { |
| 82 | + result = strings.ReplaceAll(result, l, Leetify(l[1:len(l)-1], SHA256Uint64(seed))) |
| 83 | + } |
| 84 | + |
| 85 | + return result |
| 86 | +} |
0 commit comments