python的SHA1字符串正则表达式 [英] SHA1 string regex for python
本文介绍了python的SHA1字符串正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要检查一个字符串是否是一个有效的 SHA1 字符串,比如
I need to check a string is a valid SHA1 string, like
'418c1f073782a1c855890971ff18794f7a298f6d'
我不知道相关规则,例如数字和字母是否必须?或者最少有多少个数字或字母?
I didn't know the rules for that, for example whether a number and a letter is a must? or how many number or letters are minimum?
有人可以建议任何正则表达式在 python 中进行匹配吗?
Could anybody advise any regex for matching in python?
推荐答案
我相信避免使用正则表达式会更快.SHA1 是一个随机的 40 位十六进制数,因此如果您无法将其转换为十六进制并且长度不是 40 个字符,则它不是 SHA1:
I believe it's faster to avoid using regex. A SHA1 is a random 40-digit hexidecimal number, so if you can't convert it to a hex and it's not 40 characters in length, it's not a SHA1:
def is_sha1(maybe_sha):
if len(maybe_sha) != 40:
return False
try:
sha_int = int(maybe_sha, 16)
except ValueError:
return False
return True
这篇关于python的SHA1字符串正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文