-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] string obfuscator - for swift
- Loading branch information
1 parent
ebe8afb
commit d134c25
Showing
14 changed files
with
215 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
/*.xcodeproj | ||
xcuserdata/ | ||
DerivedData/ | ||
.swiftpm/config/registries.json | ||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata | ||
.netrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// swift-tools-version: 5.7 | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "Demo", | ||
products: [ | ||
.library( | ||
name: "Demo", | ||
targets: ["Demo"]), | ||
], | ||
targets: [ | ||
.target( | ||
name: "Demo", | ||
dependencies: []), | ||
.testTarget( | ||
name: "DemoTests", | ||
dependencies: ["Demo"]), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# swift | ||
|
||
A demo project for using envject in a swift project. | ||
|
||
## To run the demo project | ||
|
||
Please see the [Secrets.swift](Sources/Demo/Secrets.swift) and unit test case [DemoTests](Tests/DemoTests/DemoTests.swift). | ||
|
||
### Without string obfuscation | ||
|
||
```bash | ||
X_API_KEY="Some secret key" go run ../../main.go --file Sources/Demo/Secrets.swift | ||
swift test | ||
``` | ||
|
||
### With string obfuscation | ||
|
||
```bash | ||
X_API_KEY="Some secret key" go run ../../main.go --file Sources/Demo/Secrets.swift --obfuscate-for=swift | ||
swift test | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
enum Secrets { | ||
static let apiKey = "${X_API_KEY}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import XCTest | ||
@testable import Demo | ||
|
||
final class DemoTests: XCTestCase { | ||
func testUnobfuscated() throws { | ||
XCTAssertEqual(Secrets.apiKey, "Some secret key") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package obfuscators | ||
|
||
func Obfuscate(str string, salt []byte) []byte { | ||
result := make([]byte, 0, len(str)) | ||
for i, char := range []byte(str) { | ||
result = append(result, char^salt[i%len(salt)]) | ||
} | ||
return result | ||
} | ||
|
||
func Deobfuscate(bytes []byte, salt []byte) (string, error) { | ||
result := make([]byte, 0, len(bytes)) | ||
for i, b := range bytes { | ||
result = append(result, b^salt[i%len(salt)]) | ||
} | ||
return string(result), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package utils | ||
|
||
import ( | ||
"crypto/rand" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func ConvertBytesToHexString(bytes []byte) string { | ||
hexStrings := make([]string, len(bytes)) | ||
for i, b := range bytes { | ||
hexStrings[i] = fmt.Sprintf("0x%02x", b) | ||
} | ||
return strings.Join(hexStrings, ", ") | ||
} | ||
|
||
func GenerateRandomBytes(size int) ([]byte, error) { | ||
b := make([]byte, size) | ||
_, err := rand.Read(b) | ||
return b, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package value_encoders | ||
|
||
type RawValueEncoder struct {} | ||
|
||
func (v *RawValueEncoder) Encode(value string) string { | ||
return value | ||
} | ||
|
||
func (v *RawValueEncoder) AdditionalCode() string { | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package value_encoders | ||
|
||
import ( | ||
"fmt" | ||
"michaelhenry/envject/obfuscators" | ||
"michaelhenry/envject/utils" | ||
) | ||
|
||
type SwiftValueEncoder struct { | ||
Salt []byte | ||
} | ||
|
||
func (v *SwiftValueEncoder) Encode(value string) string { | ||
bytes := obfuscators.Obfuscate(value, v.Salt) | ||
return fmt.Sprintf("\\({ deobfuscate([%v], salt: salt) }())", utils.ConvertBytesToHexString(bytes)) | ||
} | ||
|
||
func (v *SwiftValueEncoder) AdditionalCode() string { | ||
return fmt.Sprintf(` | ||
// MARK: - Auto generated by envject (https://github.com/michaelhenry/envject) | ||
private let salt: [UInt8] = [%s] | ||
private func deobfuscate(_ bytes: [UInt8], salt: [UInt8]) -> String { bytes.enumerated().reduce(into: "") { $0.append(Character(UnicodeScalar($1.element ^ salt[$1.offset %% salt.count])))}}`, utils.ConvertBytesToHexString(v.Salt)) | ||
} | ||
|
||
func NewSwiftValueEncoder() *SwiftValueEncoder { | ||
bytes, _ := utils.GenerateRandomBytes(16) | ||
return &SwiftValueEncoder{Salt: bytes} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package value_encoders | ||
|
||
type ValueEncoder interface { | ||
Encode(value string) string | ||
AdditionalCode() string | ||
} |