Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support to custom random seed #116

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 12 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,11 @@ 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.
techouse marked this conversation as resolved.
Show resolved Hide resolved
/// **Note**: This will make the `Random` instance non-secure!
final int? randomSeed;

const Envied({
String? path,
bool? requireEnvFile,
Expand All @@ -95,6 +100,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 +166,11 @@ 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.
techouse marked this conversation as resolved.
Show resolved Hide resolved
/// **Note**: This will make the `Random` instance non-secure!
final int? randomSeed;

const EnviedField({
this.varName,
this.obfuscate,
Expand All @@ -168,5 +179,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