代码之家  ›  专栏  ›  技术社区  ›  WebQube

python检查字符串是否是盐字符的复合字符串

  •  0
  • WebQube  · 技术社区  · 6 年前

    我想检查一下我收到的 access_token

    SALT_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'

    最好的检查方法是什么。我退房了 re ,但我认为这与此无关。 我可以构建一个函数来检查它,但是如果有一行程序可以这样做的话,我会感兴趣的

    is_build_from_salt_chars_only(token) -> returns bool true/false

    示例:

    a8B4 true

    a8#4 -> false

    6 回复  |  直到 6 年前
        1
  •  2
  •   kristaps    6 年前

    在@deceze的提示下展开:

    def is_build_from_salt_chars_only(token):
        return set(token).issubset(set(SALT_CHARS))
    
        2
  •  1
  •   Gevorg Davoian    6 年前

    def is_built_from_salt_chars_only(token):
        return all(char in SALT_CHARS for char in token)
    
        3
  •  1
  •   Igl3    6 年前

    如果你的盐字符都是字母数字的,最简单的方法就是 str 功能 .isalnum() :

    salt = "adeooAEDjifewOfej23923mklEWE"
    non_salt = "\dekoS,"
    
    print(salt.isalnum())
    # prints True
    print(non_salt.isalnum())
    # prints False
    

    SALT_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
    
    salt = "adeooAEDjifewOfej23923mklEWE"
    non_salt = "\dekoS,"
    
    # either with sets:
    print(set(salt).issubset(set(SALT_CHARS)))
    # prints True
    print(set(non_salt).issubset(set(SALT_CHARS)))
    # prints False
    
    # or with a list comprehension:
    print(all([x in SALT_CHARS for x in salt]))
    # prints True
    
    print(all([x in SALT_CHARS for x in non_salt]))
    # prints False
    
        4
  •  1
  •   rocksteady    6 年前

    re绝对是实现这一点的一种方法。请尝试以下操作:

    SALT_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
    
    def is_build_from_salt_chars_only(token):
        if re.match('^[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]+$', token, flags=0):
        return True
    else:
        return False
    
    print(is_build_from_salt_chars_only('a8B4'))
    print(is_build_from_salt_chars_only('a8#4'))
    

    re.match(r'^\w+$', token, flags=0)
    
        5
  •  0
  •   politinsa    6 年前

    可能有一种聪明而有效的方法:

    salt = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
    ascii = [ord(i) for i in a]
    ascii.sort()
    print(ascii)
    [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122]
    

    所以一个快速的方法应该是:

    for i in 'ab#4':
        if not 48 <= ord(i) < 57 and not 65 <= ord(i) <= 90 and not 97 <= ord(i) <= 122:
            return False
    return True
    

    只有在SALT不变的情况下才起作用,因此可以对值进行共享编码以加快执行时间。

        6
  •  0
  •   Haukland    6 年前

    import re
    SALT_CHARS = '^[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]*$'
    
    def salt(tested_string):
        return re.match(SALT_CHARS, tested_string) is not None
    

    .

    print(salt('asdf657'))
    #returns true
    print(salt('asdf6!57'))
    #returns false