Skip to content

Commit 4eefd16

Browse files
authored
Merge pull request #2 from salishseasoftware/develop
Rename `cipher` parameter to `token`.
2 parents be41d1c + 5d89414 commit 4eefd16

File tree

9 files changed

+74
-107
lines changed

9 files changed

+74
-107
lines changed

.github/workflows/swift.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Swift
2+
3+
on:
4+
push:
5+
branches: [ develop ]
6+
pull_request:
7+
branches: [ develop ]
8+
9+
jobs:
10+
build:
11+
12+
runs-on: macos-latest
13+
14+
steps:
15+
- uses: actions/checkout@v2
16+
- name: Build
17+
run: swift build --disable-sandbox -v
18+
- name: Run tests
19+
run: swift test -v

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Log
22

3+
## 0.1.2
4+
5+
### Changed
6+
7+
* Rename `cipher` parameter to `token`.
8+
39
## 0.1.1
410

511
Initial release

README.md

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Inspired by [twenty3/Obfuscator](https://github.com/twenty3/Obfuscator), and the
1313

1414
This package contains both a library and command line tool.
1515

16-
Use the `obfuscate` command line tool to encrypt your secret token. It generates both a cipher and a key (A.K.A. salt) you can use to reveal the original value.
16+
Use the `obfuscate` command line tool to encrypt your secret token. It generates both a token and a key (A.K.A. salt) you can use to reveal the original value.
1717

1818
Include the library in your application to decode the value at runtime.
1919

@@ -72,7 +72,7 @@ the original value using the obfuscates string and the salt.
7272
You can include the obfuscated string in your applications source code and provide the key
7373
through some type of configuration method (ENV, XCConfig file, etc).
7474
75-
Then use the `Obfuscator` library to decrypt the cipher at runtime when needed.
75+
Then use the `Obfuscator` library to decrypt the token at runtime when needed.
7676
7777
The important bit is that your original secret should not be present in your source code,
7878
config files, or your SCM system.
@@ -100,8 +100,8 @@ SUBCOMMANDS:
100100
```
101101
OVERVIEW: Obfuscates a string.
102102
103-
Generates a cipher from the provided string, along with a key that can
104-
be used to decrypt the cipher, and reveal the original value.
103+
Generates a token from the provided string, along with a key that can
104+
be used to decrypt the token, and reveal the original value.
105105
106106
USAGE: obfuscate encrypt <string>
107107
@@ -117,12 +117,12 @@ OPTIONS:
117117
```
118118
OVERVIEW: Reveals an obfuscated string.
119119
120-
Decrypts the provided cipher using the key to reveal the original value.
120+
Decrypts the provided token using the key to reveal the original value.
121121
122-
USAGE: obfuscate decrypt --cipher <cipher> --key <key>
122+
USAGE: obfuscate decrypt --token <token> --key <key>
123123
124124
OPTIONS:
125-
-c, --cipher <cipher> The obfuscated string
125+
-t, --token <token> The obfuscated string
126126
-k, --key <key> Secret key
127127
-h, --help Show help information.
128128
```
@@ -146,17 +146,17 @@ An error or type `ObfuscatorError.encryptionFailure` if the encryption fails.
146146

147147
__Returns__
148148

149-
A `(String, String)` tuple consisting of the obfuscated string (cipher) and a randomly generated salt (key) used to perform the encryption.
149+
A `(String, String)` tuple consisting of the obfuscated string (token) and a randomly generated salt (key) used to perform the encryption.
150150

151151
### decrypt
152152

153-
`decrypt(cipher:,key:)`
153+
`decrypt(token:,key:)`
154154

155155
Reveals the original value of an encrypted string.
156156

157157
__Parameters__
158158

159-
- `cipher:` The encrypted string.
159+
- `token:` The encrypted string.
160160
- `key:` The salt used to encrypt the string.
161161

162162
__Throws__
@@ -187,6 +187,3 @@ let package = Package(
187187
)
188188
```
189189

190-
191-
192-

Sources/Obfuscator/obfuscator.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,38 @@ public class Obfuscator {
1010
/// Encrypt a string
1111
/// - Parameter secret: The secret you want to encrypt
1212
/// - Throws: A ObfuscatorError.encryptionFailure if the encryption fails.
13-
/// - Returns: A tuple consisting of the encrypted string (cipher) and a
13+
/// - Returns: A tuple consisting of the encrypted string (token) and a
1414
/// randomly generated salt (key) used to perform the encryption.
15-
public static func encrypt(_ secret: String) throws -> (cipher: String, key: String) {
15+
public static func encrypt(_ secret: String) throws -> (token: String, key: String) {
1616
let key = randomString(length: secret.lengthOfBytes(using: .utf8))
1717
let salt = [UInt8](key.utf8)
1818
let bytes = [UInt8](secret.utf8)
1919

2020
// make sure salt and secret are the same length
2121
guard bytes.count == salt.count else { throw ObfuscatorError.encryptionFailure }
2222

23-
let encrypted = zip(bytes, salt)
23+
let cipher = zip(bytes, salt)
2424
.map { $0 ^ $1 }
2525
.map { String(describing: $0) }
2626
.joined(separator: delimiter)
2727
.data(using: .utf8)?
2828
.base64EncodedString()
2929

30-
guard let cipher = encrypted else { throw ObfuscatorError.encryptionFailure }
30+
guard let token = cipher else { throw ObfuscatorError.encryptionFailure }
3131

32-
return (cipher, key)
32+
return (token, key)
3333
}
3434

3535

3636
/// Reveals the original value of an encrypted string
3737
/// - Parameters:
38-
/// - cipher: The encrytped string.
38+
/// - token: The encrytped string.
3939
/// - key: The key (A.K.A. salt) used to encrypt the orignal.
4040
/// - Throws: An ObfuscatorError.decryptionFailure if the decryption fails.
4141
/// - Returns: The original value.
42-
public static func decrypt(cipher: String, key: String) throws -> String {
42+
public static func decrypt(token: String, key: String) throws -> String {
4343
guard
44-
let data = Data.init(base64Encoded: cipher),
44+
let data = Data.init(base64Encoded: token),
4545
let byteString = String.init(bytes: data, encoding: .utf8)
4646
else {
4747
throw ObfuscatorError.decryptionFailure

Sources/obfuscate/Decrypt.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ extension Obfuscate {
66
struct Decrypt: ParsableCommand {
77
static var configuration = CommandConfiguration(
88
abstract: "Reveals an obfuscated string.",
9-
discussion: "Decrypts the provided cipher using the key to reveal the original value."
9+
discussion: "Decrypts the provided token using the key to reveal the original value."
1010
)
1111

1212
@Option(name: .shortAndLong, help: "The obfuscated string")
13-
var cipher: String
13+
var token: String
1414

1515
@Option(name: .shortAndLong, help: "Secret key")
1616
var key: String
1717

1818
mutating func run() {
1919
do {
20-
let result = try Obfuscator.decrypt(cipher: cipher, key: key)
20+
let result = try Obfuscator.decrypt(token: token, key: key)
2121
print(result)
2222
} catch {
2323
Self.exit(withError: error)

Sources/obfuscate/Encrypt.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ extension Obfuscate {
77
static var configuration = CommandConfiguration(
88
abstract: "Obfuscates a string.",
99
discussion: """
10-
Generates a cipher from the provided string, along with a key that can be used to decrypt the cipher,
10+
Generates a token from the provided string, along with a key that can be used to decrypt the token,
1111
and reveal the original value.
1212
"""
1313
)
@@ -16,8 +16,8 @@ extension Obfuscate {
1616

1717
mutating func run() {
1818
do {
19-
let (cipher, key) = try Obfuscator.encrypt(string)
20-
print("cipher: \(cipher)")
19+
let (token, key) = try Obfuscator.encrypt(string)
20+
print("token: \(token)")
2121
print("key: \(key)")
2222
} catch {
2323
Self.exit(withError: error)

Sources/obfuscate/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct Obfuscate: ParsableCommand {
1313
You can include the obfuscated string in your applications source code and provide the key
1414
through some type of configuration method (ENV, XCConfig file, etc).
1515
16-
Then use the `Obfuscator` library to decrypt the cipher at runtime when needed.
16+
Then use the `Obfuscator` library to decrypt the token at runtime when needed.
1717
1818
The important bit is that your original secret should not be present in your source code,
1919
config files, or your SCM system.

Tests/ObfuscatorTests/ObsfucatorTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,30 @@ final class ObfuscatorTests: XCTestCase {
66
let sekret = "super duper top sekret 💩!"
77

88
func testEncrypt() throws {
9-
let (cipher, key) = try Obfuscator.encrypt(sekret)
10-
XCTAssertFalse(cipher.isEmpty)
9+
let (token, key) = try Obfuscator.encrypt(sekret)
10+
XCTAssertFalse(token.isEmpty)
1111
XCTAssertFalse(key.isEmpty)
12-
XCTAssertNotEqual(cipher, key)
12+
XCTAssertNotEqual(token, key)
1313
XCTAssertNotEqual(sekret, key)
1414
}
1515

1616
func testDecrypt() throws {
17-
let (cipher, key) = try Obfuscator.encrypt(sekret)
18-
let decrypted = try Obfuscator.decrypt(cipher: cipher, key: key)
17+
let (token, key) = try Obfuscator.encrypt(sekret)
18+
let decrypted = try Obfuscator.decrypt(token: token, key: key)
1919
XCTAssertNotNil(decrypted)
2020
XCTAssertEqual(sekret, decrypted)
2121
}
2222

2323
func testDecryptInvalidKey() throws {
24-
let (cipher, _) = try Obfuscator.encrypt(sekret)
25-
XCTAssertThrowsError(try Obfuscator.decrypt(cipher: cipher, key: "INVALID")) { (errorThrown) in
24+
let (token, _) = try Obfuscator.encrypt(sekret)
25+
XCTAssertThrowsError(try Obfuscator.decrypt(token: token, key: "INVALID")) { (errorThrown) in
2626
XCTAssertEqual(errorThrown as? Obfuscator.ObfuscatorError, Obfuscator.ObfuscatorError.decryptionFailure)
2727
}
2828
}
2929

3030
func testDecryptInvalidSalt() throws {
3131
let (_, key) = try Obfuscator.encrypt(sekret)
32-
XCTAssertThrowsError(try Obfuscator.decrypt(cipher: "INVALID", key: key)) { (errorThrown) in
32+
XCTAssertThrowsError(try Obfuscator.decrypt(token: "INVALID", key: key)) { (errorThrown) in
3333
XCTAssertEqual(errorThrown as? Obfuscator.ObfuscatorError, Obfuscator.ObfuscatorError.decryptionFailure)
3434
}
3535
}

0 commit comments

Comments
 (0)