|
| 1 | +import 'dart:math'; |
| 2 | + |
| 3 | +import 'package:analyzer/dart/element/element.dart'; |
| 4 | +import 'package:source_gen/source_gen.dart'; |
| 5 | + |
| 6 | +/// Generate the line to be used in the generated class. |
| 7 | +/// If [value] is `null`, it means the variable definition doesn't exist |
| 8 | +/// and an [InvalidGenerationSourceError] will be thrown. |
| 9 | +/// |
| 10 | +/// Since this function also does the type casting, |
| 11 | +/// an [InvalidGenerationSourceError] will also be thrown if |
| 12 | +/// the type can't be casted, or is not supported. |
| 13 | +String generateLineEncrypted(FieldElement field, String? value) { |
| 14 | + if (value == null) { |
| 15 | + throw InvalidGenerationSourceError( |
| 16 | + 'Environment variable not found for field `${field.name}`.', |
| 17 | + element: field, |
| 18 | + ); |
| 19 | + } |
| 20 | + |
| 21 | + final rand = Random.secure(); |
| 22 | + final type = field.type.getDisplayString(withNullability: false); |
| 23 | + final name = field.name; |
| 24 | + final keyName = '_enviedkey$name'; |
| 25 | + |
| 26 | + switch (type) { |
| 27 | + case "int": |
| 28 | + final parsed = int.tryParse(value); |
| 29 | + if (parsed == null) { |
| 30 | + throw InvalidGenerationSourceError( |
| 31 | + 'Type `$type` does not align up to value `$value`.', |
| 32 | + element: field, |
| 33 | + ); |
| 34 | + } else { |
| 35 | + final key = rand.nextInt(1 << 32); |
| 36 | + final encValue = parsed ^ key; |
| 37 | + return 'static final int $keyName = $key;\n' |
| 38 | + 'static final int $name = $keyName ^ $encValue;'; |
| 39 | + } |
| 40 | + case "bool": |
| 41 | + final lowercaseValue = value.toLowerCase(); |
| 42 | + if (['true', 'false'].contains(lowercaseValue)) { |
| 43 | + final parsed = lowercaseValue == 'true'; |
| 44 | + final key = rand.nextBool(); |
| 45 | + final encValue = parsed ^ key; |
| 46 | + return 'static final bool $keyName = $key;\n' |
| 47 | + 'static final bool $name = $keyName ^ $encValue;'; |
| 48 | + } else { |
| 49 | + throw InvalidGenerationSourceError( |
| 50 | + 'Type `$type` does not align up to value `$value`.', |
| 51 | + element: field, |
| 52 | + ); |
| 53 | + } |
| 54 | + case "String": |
| 55 | + case "dynamic": |
| 56 | + final parsed = value.codeUnits; |
| 57 | + final key = parsed.map((e) => rand.nextInt(1 << 32)).toList( |
| 58 | + growable: false, |
| 59 | + ); |
| 60 | + final encValue = List.generate(parsed.length, (i) => i, growable: false) |
| 61 | + .map((i) => parsed[i] ^ key[i]) |
| 62 | + .toList(growable: false); |
| 63 | + final encName = '_envieddata$name'; |
| 64 | + return 'static const List<int> $keyName = [${key.join(", ")}];\n' |
| 65 | + 'static const List<int> $encName = [${encValue.join(", ")}];\n' |
| 66 | + 'static final ${type == 'dynamic' ? '' : 'String'} $name = String.fromCharCodes(\n' |
| 67 | + ' List.generate($encName.length, (i) => i, growable: false)\n' |
| 68 | + ' .map((i) => $encName[i] ^ $keyName[i])\n' |
| 69 | + ' .toList(growable: false),\n' |
| 70 | + ');'; |
| 71 | + default: |
| 72 | + throw InvalidGenerationSourceError( |
| 73 | + 'Obfuscated envied can only handle types such as `int`, `bool` and `String`. Type `$type` is not one of them.', |
| 74 | + element: field, |
| 75 | + ); |
| 76 | + } |
| 77 | +} |
0 commit comments