obfuscatorSeedTransform.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 2023, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package transforms
  20. import (
  21. "encoding/hex"
  22. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  23. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  24. )
  25. type ObfuscatorSeedTransformerParameters struct {
  26. TransformName string
  27. TransformSpec Spec
  28. TransformSeed *prng.Seed
  29. }
  30. // Apply applies the transformation in-place to the given slice of bytes.
  31. // No change is made if the tranformation fails.
  32. func (t *ObfuscatorSeedTransformerParameters) Apply(b []byte) error {
  33. if t.TransformSpec == nil {
  34. return nil
  35. }
  36. input := hex.EncodeToString(b)
  37. newSeedString, err := t.TransformSpec.ApplyString(t.TransformSeed, input)
  38. if err != nil {
  39. return errors.Trace(err)
  40. }
  41. newSeed, err := hex.DecodeString(newSeedString)
  42. if err != nil {
  43. return errors.Trace(err)
  44. }
  45. if len(newSeed) != len(b) {
  46. return errors.TraceNew("invalid transform spec")
  47. }
  48. copy(b, newSeed)
  49. return nil
  50. }