How do I check if a string is a valid md5 or sha1 checksum string

前端 未结 5 591
陌清茗
陌清茗 2020-12-30 00:10

I don\'t want to calculate a file\'s checksum, just to know if a given string is a valid checksum

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-30 00:52

    There is no such thing as an MD5 or SHA-1 string, at least not one that is standardized. All you can test for is the size of the byte array: 16 for MD5 (a hash with an output size of 128 bits) or 20 bytes for SHA-1 (a hash with an output size of 160 bits) encoded using hexadecimal encoding or base 64 encoding.

    If you use md5sum then generally the checksum is shown as hexadecimal encoding, using only lowercase characters (followed by the file name or - for standard input). Hexadecimals are generally preferred, but hashes may also contain separator character or use a different encoding such as base 64 or base 64 URL (etc. etc.).

    The byte size you are testing for might however belong to an entirely different hash such as RIPEMD that may also have the same output size. Or it may be another value with the same amount of bytes.

提交回复
热议问题