Skip to content

Commit ec95865

Browse files
committed
refactor: remove no-op randomness from key derivation
1 parent ccb5eeb commit ec95865

File tree

3 files changed

+10
-30
lines changed

3 files changed

+10
-30
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ Expands to (simplified):
9090
@inline(__always)
9191
func $_deriveKey(_ u: [UInt8], _ i: UInt8) -> UInt8 {
9292
var h: UInt8 = i
93-
let r = Int.random(in: 0 ... 16)
94-
let s = r > 15 ? r : 16
95-
for b in u[0 ..< s] {
93+
for b in u {
9694
h = (h &+ (b &* 31)) ^ 0x5D
9795
}
9896
return h

Sources/ObfuStringMacros/ObfuscationAlgorithms/Obfuscation.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ enum Obfuscation: CaseIterable {
4545
@inline(__always)
4646
func \(funcName)(_ u: [UInt8], _ i: UInt8) -> UInt8 {
4747
var h: UInt8 = i
48-
let r = Int.random(in: 0...16)
49-
let s = r > 15 ? r : 16
50-
for b in u[0..<s] {
48+
for b in u {
5149
h = (h &+ (b &* 31)) ^ 0x5D
5250
}
5351
return h

Tests/ObfuStringTests/ObfuStringMacroTests.swift

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ final class ObfuStringMacroAlgoTests: XCTestCase {
3030
@inline(__always)
3131
func deriveKeyFn(_ u: [UInt8], _ i: UInt8) -> UInt8 {
3232
var h: UInt8 = i
33-
let r = Int.random(in: 0 ... 16)
34-
let s = r > 15 ? r : 16
35-
for b in u[0 ..< s] {
33+
for b in u {
3634
h = (h &+ (b &* 31)) ^ 0x5D
3735
}
3836
return h
@@ -80,9 +78,7 @@ final class ObfuStringMacroAlgoTests: XCTestCase {
8078
@inline(__always)
8179
func deriveKeyFn(_ u: [UInt8], _ i: UInt8) -> UInt8 {
8280
var h: UInt8 = i
83-
let r = Int.random(in: 0 ... 16)
84-
let s = r > 15 ? r : 16
85-
for b in u[0 ..< s] {
81+
for b in u {
8682
h = (h &+ (b &* 31)) ^ 0x5D
8783
}
8884
return h
@@ -127,9 +123,7 @@ final class ObfuStringMacroAlgoTests: XCTestCase {
127123
@inline(__always)
128124
func deriveKeyFn(_ u: [UInt8], _ i: UInt8) -> UInt8 {
129125
var h: UInt8 = i
130-
let r = Int.random(in: 0 ... 16)
131-
let s = r > 15 ? r : 16
132-
for b in u[0 ..< s] {
126+
for b in u {
133127
h = (h &+ (b &* 31)) ^ 0x5D
134128
}
135129
return h
@@ -175,9 +169,7 @@ final class ObfuStringMacroAlgoTests: XCTestCase {
175169
@inline(__always)
176170
func deriveKeyFn(_ u: [UInt8], _ i: UInt8) -> UInt8 {
177171
var h: UInt8 = i
178-
let r = Int.random(in: 0 ... 16)
179-
let s = r > 15 ? r : 16
180-
for b in u[0 ..< s] {
172+
for b in u {
181173
h = (h &+ (b &* 31)) ^ 0x5D
182174
}
183175
return h
@@ -230,9 +222,7 @@ final class ObfuStringMacroAlgoTests: XCTestCase {
230222
@inline(__always)
231223
func deriveKeyFn(_ u: [UInt8], _ i: UInt8) -> UInt8 {
232224
var h: UInt8 = i
233-
let r = Int.random(in: 0 ... 16)
234-
let s = r > 15 ? r : 16
235-
for b in u[0 ..< s] {
225+
for b in u {
236226
h = (h &+ (b &* 31)) ^ 0x5D
237227
}
238228
return h
@@ -259,9 +249,7 @@ final class ObfuStringMacroAlgoTests: XCTestCase {
259249
@inline(__always)
260250
func deriveKeyFn(_ u: [UInt8], _ i: UInt8) -> UInt8 {
261251
var h: UInt8 = i
262-
let r = Int.random(in: 0 ... 16)
263-
let s = r > 15 ? r : 16
264-
for b in u[0 ..< s] {
252+
for b in u {
265253
h = (h &+ (b &* 31)) ^ 0x5D
266254
}
267255
return h
@@ -310,9 +298,7 @@ final class ObfuStringMacroAlgoTests: XCTestCase {
310298
@inline(__always)
311299
func deriveKeyFn(_ u: [UInt8], _ i: UInt8) -> UInt8 {
312300
var h: UInt8 = i
313-
let r = Int.random(in: 0 ... 16)
314-
let s = r > 15 ? r : 16
315-
for b in u[0 ..< s] {
301+
for b in u {
316302
h = (h &+ (b &* 31)) ^ 0x5D
317303
}
318304
return h
@@ -358,9 +344,7 @@ final class ObfuStringMacroAlgoTests: XCTestCase {
358344
@inline(__always)
359345
func deriveKeyFn(_ u: [UInt8], _ i: UInt8) -> UInt8 {
360346
var h: UInt8 = i
361-
let r = Int.random(in: 0 ... 16)
362-
let s = r > 15 ? r : 16
363-
for b in u[0 ..< s] {
347+
for b in u {
364348
h = (h &+ (b &* 31)) ^ 0x5D
365349
}
366350
return h

0 commit comments

Comments
 (0)