You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift中实现固定模式的UID字符串混淆(无需第三方库)

Swift UID Obfuscation: Consistent, Library-Free, Fixed-Pattern Solution

Got it, let's break down how to solve this exactly as you need it. You want a consistent, non-random UID obfuscation in Swift that doesn't require CryptoSwift, keeps the same output across devices, and ensures the same user always maps to the same obfuscated value for fixed database positioning.

Since you're only looking to reduce readability (not full encryption), here are two solid, library-free approaches that meet all your requirements:


Approach 1: Fixed Character Mapping Table

This uses a pre-defined, static map to replace each character in the UID with another fixed character. The same input will always produce the same output, no matter the device.

Code Implementation

// Define a fixed, static character map (keep this consistent across all your code)
private let uidCharMap: [Character: Character] = [
    "0": "7", "1": "3", "2": "9", "3": "1", "4": "6",
    "5": "2", "6": "4", "7": "0", "8": "8", "9": "5",
    "a": "k", "b": "m", "c": "p", "d": "r", "e": "t",
    "f": "v", "g": "x", "h": "z", "i": "b", "j": "d",
    "k": "f", "l": "h", "m": "j", "n": "l", "o": "n",
    "p": "a", "q": "c", "r": "e", "s": "g", "t": "i",
    "u": "o", "v": "q", "w": "s", "x": "u", "y": "w", "z": "y",
    "A": "K", "B": "M", "C": "P", "D": "R", "E": "T",
    "F": "V", "G": "X", "H": "Z", "I": "B", "J": "D",
    "K": "F", "L": "H", "M": "J", "N": "L", "O": "N",
    "P": "A", "Q": "C", "R": "E", "S": "G", "T": "I",
    "U": "O", "V": "Q", "W": "S", "X": "U", "Y": "W", "Z": "Y"
]

// Obfuscate function
func obfuscateUID(_ rawUID: String) -> String {
    return rawUID.map { uidCharMap[$0] ?? $0 }.joined()
}

// De-obfuscate function (if you ever need to reverse it)
func deobfuscateUID(_ obfuscatedUID: String) -> String {
    let reverseMap = uidCharMap.reduce(into: [Character: Character]()) { $0[$1.value] = $1.key }
    return obfuscatedUID.map { reverseMap[$0] ?? $0 }.joined()
}

Why This Works

  • Consistency: The map is static, so the same UID will always turn into the same obfuscated string across every device.
  • No Dependencies: No external libraries needed—just plain Swift.
  • Readability Reduction: The random-looking character swap makes the UID unrecognizable at a glance, while still preserving a 1:1 mapping for fixed database positioning.

Approach 2: Fixed ASCII Offset (ROT-Style Shift)

This shifts each character's ASCII value by a fixed number, with wrap-around for letters/numbers. It's simple, fast, and fully consistent.

Code Implementation

// Use a fixed offset (pick any number you like—13 is ROT13, a common choice)
private let fixedOffset: UInt8 = 13

func obfuscateUID(_ rawUID: String) -> String {
    var obfuscatedChars = [Character]()
    
    for char in rawUID.utf8 {
        var shiftedValue: UInt8
        
        // Handle different character categories to avoid breaking non-alphanumerics
        switch char {
        case 48...57: // 0-9
            shiftedValue = (char - 48 + fixedOffset) % 10 + 48
        case 65...90: // A-Z
            shiftedValue = (char - 65 + fixedOffset) % 26 + 65
        case 97...122: // a-z
            shiftedValue = (char - 97 + fixedOffset) % 26 + 97
        default:
            // Leave non-alphanumeric characters unchanged (adjust if needed)
            shiftedValue = char
        }
        
        obfuscatedChars.append(Character(UnicodeScalar(shiftedValue)))
    }
    
    return String(obfuscatedChars)
}

// De-obfuscate function
func deobfuscateUID(_ obfuscatedUID: String) -> String {
    var rawChars = [Character]()
    
    for char in obfuscatedUID.utf8 {
        var shiftedValue: UInt8
        
        switch char {
        case 48...57:
            shiftedValue = (char - 48 + 10 - fixedOffset % 10) % 10 + 48
        case 65...90:
            shiftedValue = (char - 65 + 26 - fixedOffset % 26) % 26 + 65
        case 97...122:
            shiftedValue = (char - 97 + 26 - fixedOffset % 26) % 26 + 97
        default:
            shiftedValue = char
        }
        
        rawChars.append(Character(UnicodeScalar(shiftedValue)))
    }
    
    return String(rawChars)
}

Why This Works

  • Cross-Device Consistency: The fixed offset ensures identical output for the same UID everywhere.
  • Lightweight: No libraries, minimal computation.
  • Predictable Mapping: Each UID maps to exactly one obfuscated value, so your database positioning stays fixed for each user.

Key Notes

  • These are obfuscation techniques, not encryption. They're designed to make UIDs unreadable at a glance, not secure against intentional decryption. Perfect for your use case.
  • To make it slightly stronger, you can combine both approaches: first shift the ASCII values, then apply the character map.
  • Ensure your mapping table or offset value is hardcoded consistently across all your app's targets—don't change it later, or you'll break existing UID mappings in the database.

内容的提问来源于stack exchange,提问作者Giovanni Palusa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:23:04