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

Python2仿冒用户名检测脚本开发求助:特定字符替换类仿冒识别

Python 2 Username Spoof Detection Solution

Hey there! Let’s work through this username spoof detection script you’re building. Here’s a straightforward, effective approach to implement the logic you need:

Core Approach

The key idea is to standardize characters that are commonly used to spoof others, then compare the standardized versions of the two usernames. Here’s the step-by-step breakdown:

  • First, validate that both usernames have the same length (if not, they can’t be spoofs).
  • Define a mapping of easily confused characters to a shared "standard" character (e.g., map both 0 and O to O, both I and l to l).
  • Convert each character in both usernames to its standard form, then check if all converted characters match.

Full Python 2 Code Implementation

def is_spoofed_username(username1, username2):
    # Early exit if lengths don't match
    if len(username1) != len(username2):
        return False
    
    # Mapping of confused characters to a standard equivalent
    confusion_map = {
        '0': 'O',
        'O': 'O',
        'I': 'l',
        'l': 'l',
        # Add more spoof pairs here as needed:
        # '1': 'l',
        # '8': 'B',
        # '5': 'S'
    }
    
    # Compare each character pair after standardization
    for char1, char2 in zip(username1, username2):
        # Get standard char (fallback to original if not in map)
        std_char1 = confusion_map.get(char1, char1)
        std_char2 = confusion_map.get(char2, char2)
        
        if std_char1 != std_char2:
            return False
    
    # All characters matched after standardization
    return True

# Test the function with your examples
print(is_spoofed_username('allison', 'aIIison'))  # Output: True
print(is_spoofed_username('john_doe', 'j0hn_d0e'))# Output: True
print(is_spoofed_username('alice', 'alic3'))      # Output: False
print(is_spoofed_username('bob', 'bobby'))        # Output: False

How This Works

  • Length Check: We first check if the usernames are the same length—this is a fast way to rule out non-spoof cases without extra processing.
  • Confusion Mapping: The confusion_map ensures that any character used to spoof another gets converted to the same standard value. For example, I (uppercase i) and l (lowercase L) both become l, so comparing them will register as a match.
  • Character-by-Character Comparison: We iterate through each pair of characters, convert them to their standard form, and check for matches. If any pair fails, we immediately return False.

Extending the Script

If you need to cover more spoof scenarios, just add new entries to the confusion_map. For example, to catch 1 being used as l or 5 being used as S, simply add those key-value pairs to the dictionary.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:25:41