Skip to content

Commit

Permalink
feat: add support to custom random seed
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmartos96 committed Oct 31, 2024
1 parent bf60c6d commit c9d9453
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/envied/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Unreleased

- **FEAT**: add support for custom seed when using Random during the obfuscation.

## 0.5.4+1

- **FIX**: fix parsing lines with multiple equal signs (#100)
Expand Down
10 changes: 10 additions & 0 deletions packages/envied/lib/src/envied_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ final class Envied {
/// Escapes single quotes and newlines in the value.
final bool rawStrings;

/// A seed can be provided if the obfuscation randomness needs to remain
/// reproducible across builds.
final int? randomSeed;

const Envied({
String? path,
bool? requireEnvFile,
Expand All @@ -95,6 +99,7 @@ final class Envied {
this.useConstantCase = false,
this.interpolate = true,
this.rawStrings = false,
this.randomSeed,
}) : path = path ?? '.env',
requireEnvFile = requireEnvFile ?? false;
}
Expand Down Expand Up @@ -160,6 +165,10 @@ final class EnviedField {
/// Escapes single quotes and newlines in the value.
final bool? rawString;

/// A seed can be provided if the obfuscation randomness needs to remain
/// reproducible across builds.
final int? randomSeed;

const EnviedField({
this.varName,
this.obfuscate,
Expand All @@ -168,5 +177,6 @@ final class EnviedField {
this.useConstantCase,
this.interpolate,
this.rawString,
this.randomSeed,
});
}
10 changes: 10 additions & 0 deletions packages/envied/test/envy_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ void main() {
final envied = Envied(obfuscate: true);
expect(envied.obfuscate, true);
});

test('Specified randomSeed', () {
final envied = Envied(randomSeed: 123);
expect(envied.randomSeed, 123);
});
});

group('EnviedField Test Group', () {
Expand Down Expand Up @@ -65,6 +70,11 @@ void main() {
final enviedField = EnviedField(useConstantCase: true);
expect(enviedField.useConstantCase, isTrue);
});

test('Specified randomSeed', () {
final enviedField = EnviedField(randomSeed: 123);
expect(enviedField.randomSeed, 123);
});
});

group('EnviedField Test Group with defaultValue', () {
Expand Down
4 changes: 4 additions & 0 deletions packages/envied_generator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Unreleased

- **FEAT**: add support for custom seed when using Random during the obfuscation.

## 0.5.4+1

- **FIX**: fix parsing lines with multiple equal signs (#100)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ Iterable<Field> generateFieldsEncrypted(
FieldElement field,
String? value, {
bool allowOptional = false,
int? randomSeed,
}) {
final Random rand = Random.secure();
final Random rand = randomSeed != null ? Random(randomSeed) : Random.secure();
final String type = field.type.getDisplayString(withNullability: false);
final String keyName = '_enviedkey${field.name}';
final bool isNullable = allowOptional &&
Expand Down
3 changes: 3 additions & 0 deletions packages/envied_generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ final class EnviedGenerator extends GeneratorForAnnotation<Envied> {
annotation.read('useConstantCase').literalValue as bool? ?? false,
interpolate: annotation.read('interpolate').literalValue as bool? ?? true,
rawStrings: annotation.read('rawStrings').literalValue as bool? ?? false,
randomSeed: annotation.read('randomSeed').literalValue as int?,
);

final Map<String, EnvVal> envs =
Expand Down Expand Up @@ -157,6 +158,8 @@ final class EnviedGenerator extends GeneratorForAnnotation<Envied> {
field,
interpolate ? varValue?.interpolated : varValue?.raw,
allowOptional: optional,
randomSeed: (reader.read('randomSeed').literalValue as int?) ??
config.randomSeed,
)
: generateFields(
field,
Expand Down

0 comments on commit c9d9453

Please sign in to comment.