-
Notifications
You must be signed in to change notification settings - Fork 13
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
Added obfuscation for keys #67
Changes from 2 commits
877dda3
8e6d2dc
78cf028
d5e06c3
24569f9
5d7aca1
e3c366c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// | ||
// ObfuscatedKey.swift | ||
// UtiliKit-iOS | ||
// | ||
// Created by Russell Mirabelli on 7/23/19. | ||
// Copyright © 2019 CocoaPods. All rights reserved. | ||
// | ||
|
||
// Inspired by https://github.com/onmyway133/Arcane/blob/master/Sources/Arcane/Obfuscator.swift | ||
|
||
import Foundation | ||
|
||
/// One should never commit keys directly into one's source; this is an unsafe practice. | ||
/// When it is impossible to avoid doing so, the key should at least be obfuscated. | ||
/// By using the ObfuscatedKey struct, you can build a human-readable key that nonetheless | ||
/// will not appear simply by running "strings" against your compiled code, and will even | ||
/// not appear as a string within your source code. | ||
struct ObfuscatedKey { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will need to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
private let _value: String | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we lose the internal variable and use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's not available on a "let"; I believe that the immutability of a "let" is important to make the general builder technique make sense. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair point - withdrawn. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't help but feel like I'm missing something here - what does the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair point. Since my merge is still blocked by the license (ugh), I'll think about this and see if there is a good reason I can find. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reviewed, changed. |
||
|
||
init() { | ||
self._value = "" | ||
} | ||
|
||
init(_ value: String) { | ||
self._value = value | ||
} | ||
|
||
var value: String { | ||
get { | ||
return _value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use the implicit getter here instead of manually specifying the var value: String {
return _value
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
} | ||
|
||
// codebeat:disable[TOO_MANY_FUNCTIONS] | ||
var A: ObfuscatedKey { return ObfuscatedKey(_value + "A") } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might make sense to throw these into a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice way to avoid all the publics. Done, and thanks! |
||
var B: ObfuscatedKey { return ObfuscatedKey(_value + "B") } | ||
var C: ObfuscatedKey { return ObfuscatedKey(_value + "C") } | ||
var D: ObfuscatedKey { return ObfuscatedKey(_value + "D") } | ||
var E: ObfuscatedKey { return ObfuscatedKey(_value + "E") } | ||
var F: ObfuscatedKey { return ObfuscatedKey(_value + "F") } | ||
var G: ObfuscatedKey { return ObfuscatedKey(_value + "G") } | ||
var H: ObfuscatedKey { return ObfuscatedKey(_value + "H") } | ||
var I: ObfuscatedKey { return ObfuscatedKey(_value + "I") } | ||
var J: ObfuscatedKey { return ObfuscatedKey(_value + "J") } | ||
var K: ObfuscatedKey { return ObfuscatedKey(_value + "K") } | ||
var L: ObfuscatedKey { return ObfuscatedKey(_value + "L") } | ||
var M: ObfuscatedKey { return ObfuscatedKey(_value + "M") } | ||
var N: ObfuscatedKey { return ObfuscatedKey(_value + "N") } | ||
var O: ObfuscatedKey { return ObfuscatedKey(_value + "O") } | ||
var P: ObfuscatedKey { return ObfuscatedKey(_value + "P") } | ||
var Q: ObfuscatedKey { return ObfuscatedKey(_value + "Q") } | ||
var R: ObfuscatedKey { return ObfuscatedKey(_value + "R") } | ||
var S: ObfuscatedKey { return ObfuscatedKey(_value + "S") } | ||
var T: ObfuscatedKey { return ObfuscatedKey(_value + "T") } | ||
var U: ObfuscatedKey { return ObfuscatedKey(_value + "U") } | ||
var V: ObfuscatedKey { return ObfuscatedKey(_value + "V") } | ||
var W: ObfuscatedKey { return ObfuscatedKey(_value + "W") } | ||
var X: ObfuscatedKey { return ObfuscatedKey(_value + "X") } | ||
var Y: ObfuscatedKey { return ObfuscatedKey(_value + "Y") } | ||
var Z: ObfuscatedKey { return ObfuscatedKey(_value + "Z") } | ||
|
||
var a: ObfuscatedKey { return ObfuscatedKey(_value + "a") } | ||
var b: ObfuscatedKey { return ObfuscatedKey(_value + "b") } | ||
var c: ObfuscatedKey { return ObfuscatedKey(_value + "c") } | ||
var d: ObfuscatedKey { return ObfuscatedKey(_value + "d") } | ||
var e: ObfuscatedKey { return ObfuscatedKey(_value + "e") } | ||
var f: ObfuscatedKey { return ObfuscatedKey(_value + "f") } | ||
var g: ObfuscatedKey { return ObfuscatedKey(_value + "g") } | ||
var h: ObfuscatedKey { return ObfuscatedKey(_value + "h") } | ||
var i: ObfuscatedKey { return ObfuscatedKey(_value + "i") } | ||
var j: ObfuscatedKey { return ObfuscatedKey(_value + "j") } | ||
var k: ObfuscatedKey { return ObfuscatedKey(_value + "k") } | ||
var l: ObfuscatedKey { return ObfuscatedKey(_value + "l") } | ||
var m: ObfuscatedKey { return ObfuscatedKey(_value + "m") } | ||
var n: ObfuscatedKey { return ObfuscatedKey(_value + "n") } | ||
var o: ObfuscatedKey { return ObfuscatedKey(_value + "o") } | ||
var p: ObfuscatedKey { return ObfuscatedKey(_value + "p") } | ||
var q: ObfuscatedKey { return ObfuscatedKey(_value + "q") } | ||
var r: ObfuscatedKey { return ObfuscatedKey(_value + "r") } | ||
var s: ObfuscatedKey { return ObfuscatedKey(_value + "s") } | ||
var t: ObfuscatedKey { return ObfuscatedKey(_value + "t") } | ||
var u: ObfuscatedKey { return ObfuscatedKey(_value + "u") } | ||
var v: ObfuscatedKey { return ObfuscatedKey(_value + "v") } | ||
var w: ObfuscatedKey { return ObfuscatedKey(_value + "w") } | ||
var x: ObfuscatedKey { return ObfuscatedKey(_value + "x") } | ||
var y: ObfuscatedKey { return ObfuscatedKey(_value + "y") } | ||
var z: ObfuscatedKey { return ObfuscatedKey(_value + "z") } | ||
|
||
var n0: ObfuscatedKey { return ObfuscatedKey(_value + "0") } | ||
var n1: ObfuscatedKey { return ObfuscatedKey(_value + "1") } | ||
var n2: ObfuscatedKey { return ObfuscatedKey(_value + "2") } | ||
var n3: ObfuscatedKey { return ObfuscatedKey(_value + "3") } | ||
var n4: ObfuscatedKey { return ObfuscatedKey(_value + "4") } | ||
var n5: ObfuscatedKey { return ObfuscatedKey(_value + "5") } | ||
var n6: ObfuscatedKey { return ObfuscatedKey(_value + "6") } | ||
var n7: ObfuscatedKey { return ObfuscatedKey(_value + "7") } | ||
var n8: ObfuscatedKey { return ObfuscatedKey(_value + "8") } | ||
var n9: ObfuscatedKey { return ObfuscatedKey(_value + "9") } | ||
|
||
var dot: ObfuscatedKey { return ObfuscatedKey(_value + ".") } | ||
var dash: ObfuscatedKey { return ObfuscatedKey(_value + "-") } | ||
var underscore: ObfuscatedKey { return ObfuscatedKey(_value + "_") } | ||
|
||
func anything(_ extra: String) -> ObfuscatedKey { | ||
return ObfuscatedKey(_value + extra) | ||
} | ||
// codebeat:enable[TOO_MANY_FUNCTIONS] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// ObfuscationTests.swift | ||
// UtiliKit-iOSTests | ||
// | ||
// Created by Russell Mirabelli on 7/23/19. | ||
// Copyright © 2019 CocoaPods. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
@testable import UtiliKit | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm thinking a regular There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
class ObfuscationTests: XCTestCase { | ||
|
||
func testSimpleObfuscatedString() { | ||
let key = ObfuscatedKey().A.B.A.B.value | ||
let expected = "ABAB" | ||
XCTAssert(key == expected, "Keys do not match: \(key) is not \(expected)") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very minor, but you might consider using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
|
||
func testUppercase() { | ||
let key = ObfuscatedKey().A.B.C.D.E.F.G.H.I.J.K.L.M.N.O.P.Q.R.S.T.U.V.W.X.Y.Z.value | ||
let expected = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
XCTAssert(key == expected, "Keys do not match: \(key) is not \(expected)") | ||
} | ||
|
||
func testLowercase() { | ||
let key = ObfuscatedKey().a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.value | ||
let expected = "abcdefghijklmnopqrstuvwxyz" | ||
XCTAssert(key == expected, "Keys do not match: \(key) is not \(expected)") | ||
} | ||
|
||
func testDigits() { | ||
let key = ObfuscatedKey().n0.n1.n2.n3.n4.n5.n6.n7.n8.n9.value | ||
let expected = "0123456789" | ||
XCTAssert(key == expected, "Keys do not match: \(key) is not \(expected)") | ||
} | ||
|
||
func testExtras() { | ||
let key = ObfuscatedKey().dot.dash.underscore.anything("=").value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very minor, but we might consider renaming
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kinda like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used literal. |
||
let expected = ".-_=" | ||
XCTAssert(key == expected, "Keys do not match: \(key) is not \(expected)") | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like we need to update the copyright here to "Bottle Rocket Studios". Could you go ahead and update this comment and make the change in the project file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.