Skip to content

Commit

Permalink
Update Swift and Kotlin previews and add usage code snippet.
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerluan committed Dec 22, 2023
1 parent bbea84c commit 92296b6
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 5 deletions.
56 changes: 51 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,53 @@

# Requirements

## Android

Your project must be using the Gradle Build Tool.

## iOS

Your project must be using Swift Package Manager or CocoaPods as dependency manager (or both). No support for Carthage.

<sub>Note: this gem was only tested in macOS environments.</sub>

## Android
## Preview

Your project must be using the Gradle Build Tool.
### Kotlin

## Preview
<details><summary>Click here to see the Kotlin preview</summary>
<p>

The image below shows how the auto-generated file looks like.

<details><summary>Click here to show an image with the preview!</summary>
<div align="center">
<img src="docs/kotlin-demo.png">
</div>

</p>
</details>

Usage using the example code above:

```kotlin
import com.arkanakeys.MySecrets

// Designed with testability and DI in mind
println(MySecrets.Global.someBooleanSecret)
println(MySecrets.Global.someIntSecret)
println(MySecrets.Global.mySecretAPIKey)

// Simulating environment selection using a random boolean value
val keys = if (Math.random() < 0.5) MySecrets.Dev else MySecrets.Staging
println(keys.serviceKey)
```

### Swift

<details><summary>Click here to see the Swift preview</summary>
<p>

The image below shows how the auto-generated file looks like. At the bottom of it you can see how you'll consume the code generated.
The image below shows how the auto-generated file looks like.

<div align="center">
<img src="docs/swift-demo.png">
Expand All @@ -61,6 +92,21 @@ The image below shows how the auto-generated file looks like. At the bottom of i
</p>
</details>

Usage using the example code above:

```swift
import ArkanaKeys

// Designed with testability and DI in mind
print(MySecrets.Global().someBooleanSecret)
print(MySecrets.Global().someIntSecret)
print(MySecrets.Global().mySecretAPIKey)

// This is a demo, so we are using Bool.random() to simulate the environment
let keys: MySecretsEnvironmentProtocol = Bool.random() ? MySecrets.Dev() : MySecrets.Staging()
print(keys.serviceKey)
```

# Installation

Add this gem to your `Gemfile` if you're using [bundler](https://bundler.io) (recommended):
Expand Down
58 changes: 58 additions & 0 deletions docs/demo-used-to-gen-image.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// DO NOT MODIFY
// Automatically generated by Arkana (https://github.com/rogerluan/arkana)
package com.arkanakeys

object MySecrets {
private val salt = listOf(0x3f, 0xb6, 0x2, 0x3, 0xbf, 0x54, 0xf5, 0x67, 0x95, 0xc, 0x56, 0x47, 0x87, 0x55, 0x60, 0x74, 0x6, 0x77, 0x8b, 0xd6, 0x88, 0x41, 0x99, 0xe2, 0x97, 0x92, 0x9f, 0x68, 0x7d, 0x6c, 0x39, 0x64, 0xca, 0x98, 0xe7, 0x8d, 0xe8, 0x9e, 0x1f, 0xe5, 0xad, 0x45, 0x32, 0xac, 0xc5, 0xe1, 0xf6, 0x4f, 0x67, 0xcc, 0x6a, 0xee, 0x66, 0xac, 0x80, 0xea, 0x78, 0x1b, 0xd6, 0x78, 0x4, 0x97, 0xfa, 0xcc)

internal fun decode(encoded: List<Int>, cipher: List<Int>): String {
val decoded = encoded.mapIndexed { index, item ->
(item xor cipher[(index % cipher.size)]).toByte()
}.toByteArray()
return decoded.toString(Charsets.UTF_8)
}

internal fun decodeInt(encoded: List<Int>, cipher: List<Int>): Int {
return decode(encoded = encoded, cipher = cipher).toInt()
}

internal fun decodeBoolean(encoded: List<Int>, cipher: List<Int>): Boolean {
return decode(encoded = encoded, cipher = cipher).toBoolean()
}

object Global {
val someBooleanSecret: Boolean
get() {
val encoded = listOf(0x4b, 0xc4, 0x77, 0x66)
return decodeBoolean(encoded = encoded, cipher = salt)
}

val someIntSecret: Int
get() {
val encoded = listOf(0xb, 0x84)
return decodeInt(encoded = encoded, cipher = salt)
}

val mySecretAPIKey: String
get() {
val encoded = listOf(0x6, 0x84, 0x30, 0x30, 0x8c, 0x63, 0xc7, 0x57, 0xa6, 0x3a, 0x6e, 0x72, 0xb3, 0x62, 0x57, 0x41, 0x3e, 0x47, 0xbc, 0xef, 0xba, 0x73, 0xaa, 0xd1, 0xa0, 0xa0, 0xaf, 0x5b, 0x4b, 0x54, 0xc, 0x50, 0xfd, 0xaf, 0xd2, 0xb5, 0xd8, 0xa9)
return decode(encoded = encoded, cipher = salt)
}
}

object Dev : MySecretsEnvironment {
override val serviceKey: String
get() {
val encoded = listOf(0x4b, 0xde, 0x6b, 0x70, 0x9f, 0x30, 0x90, 0x11, 0xb5, 0x67, 0x33, 0x3e, 0xa7, 0x3c, 0x13, 0x54, 0x75, 0x12, 0xe8, 0xa4, 0xed, 0x35)
return decode(encoded = encoded, cipher = salt)
}
}

object Staging : MySecretsEnvironment {
override val serviceKey: String
get() {
val encoded = listOf(0x4b, 0xde, 0x6b, 0x70, 0x9f, 0x27, 0x81, 0x6, 0xf2, 0x65, 0x38, 0x20, 0xa7, 0x3e, 0x5, 0xd, 0x26, 0x1e, 0xf8, 0xf6, 0xfb, 0x24, 0xfa, 0x90, 0xf2, 0xe6)
return decode(encoded = encoded, cipher = salt)
}
}
}
85 changes: 85 additions & 0 deletions docs/demo-used-to-gen-image.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// DO NOT MODIFY
// Automatically generated by Arkana (https://github.com/rogerluan/arkana)

import Foundation
import MySecretsInterfaces

public enum MySecrets {
@inline(__always)
fileprivate static let salt: [UInt8] = [
0x47, 0x7c, 0xe3, 0x7f, 0x80, 0xa8, 0x41, 0x6c, 0x2, 0xc3, 0x7f, 0x3f, 0x63, 0xd9, 0xb6, 0x57, 0x3a, 0x33, 0x98, 0xed, 0xfa, 0x71, 0xcb, 0x9a, 0x55, 0x23, 0x52, 0x1c, 0x31, 0xc0, 0x74, 0xd4, 0x7e, 0x6e, 0xf5, 0xca, 0xbc, 0x25, 0x69, 0xcd, 0x9, 0, 0xcb, 0x70, 0xd3, 0x5e, 0xa5, 0x92, 0xe7, 0x6b, 0x2, 0x90, 0x29, 0xc2, 0x44, 0xc8, 0x5a, 0xc2, 0xc0, 0xc7, 0x42, 0x30, 0x40, 0xa3
]

static func decode(encoded: [UInt8], cipher: [UInt8]) -> String {
return String(decoding: encoded.enumerated().map { offset, element in
element ^ cipher[offset % cipher.count]
}, as: UTF8.self)
}

static func decode(encoded: [UInt8], cipher: [UInt8]) -> Bool {
let stringValue: String = Self.decode(encoded: encoded, cipher: cipher)
return Bool(stringValue)!
}

static func decode(encoded: [UInt8], cipher: [UInt8]) -> Int {
let stringValue: String = Self.decode(encoded: encoded, cipher: cipher)
return Int(stringValue)!
}
}

public extension MySecrets {
struct Global: MySecretsGlobalProtocol {
public init() {}

@inline(__always)
public lazy var someBooleanSecret: Bool = {
let encoded: [UInt8] = [
0x33, 0xe, 0x96, 0x1a
]
return MySecrets.decode(encoded: encoded, cipher: MySecrets.salt)
}()

@inline(__always)
public lazy var someIntSecret: Int = {
let encoded: [UInt8] = [
0x73, 0x4e
]
return MySecrets.decode(encoded: encoded, cipher: MySecrets.salt)
}()

@inline(__always)
public lazy var mySecretAPIKey: String = {
let encoded: [UInt8] = [
0x7e, 0x4e, 0xd1, 0x4c, 0xb3, 0x9f, 0x73, 0x5c, 0x31, 0xf5, 0x47, 0xa, 0x57, 0xee, 0x81, 0x62, 0x2, 0x3, 0xaf, 0xd4, 0xc8, 0x43, 0xf8, 0xa9, 0x62, 0x11, 0x62, 0x2f, 0x7, 0xf8, 0x41, 0xe0, 0x49, 0x59, 0xc0, 0xf2, 0x8c, 0x12
]
return MySecrets.decode(encoded: encoded, cipher: MySecrets.salt)
}()
}
}

public extension MySecrets {
struct Dev: MySecretsEnvironmentProtocol {
public init() {}

@inline(__always)
public lazy var serviceKey: String = {
let encoded: [UInt8] = [
0x33, 0x14, 0x8a, 0xc, 0xa0, 0xcc, 0x24, 0x1a, 0x22, 0xa8, 0x1a, 0x46, 0x43, 0xb0, 0xc5, 0x77, 0x49, 0x56, 0xfb, 0x9f, 0x9f, 0x5
]
return MySecrets.decode(encoded: encoded, cipher: MySecrets.salt)
}()
}
}
public extension MySecrets {
struct Staging: MySecretsEnvironmentProtocol {
public init() {}

@inline(__always)
public lazy var serviceKey: String = {
let encoded: [UInt8] = [
0x33, 0x14, 0x8a, 0xc, 0xa0, 0xdb, 0x35, 0xd, 0x65, 0xaa, 0x11, 0x58, 0x43, 0xb2, 0xd3, 0x2e, 0x1a, 0x5a, 0xeb, 0xcd, 0x89, 0x14, 0xa8, 0xe8, 0x30, 0x57
]
return MySecrets.decode(encoded: encoded, cipher: MySecrets.salt)
}()
}
}
Binary file added docs/kotlin-demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/swift-demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 92296b6

Please sign in to comment.