0

I need a regex for android signature hash which is used in azure in a field presented on picture.

I used something like that:

"^(?=.{28}$)(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$"

It's matching words base64-encoded with 28 characters words but it will also match e.g. platformAndroidSignatureHash which azure decline (presented on a picture).

WORKING EXAMPLE:

  • 2pmj9i4rSx0yEb/viWBYkE/ZQrk=
  • aPz8/NARbPz8pPzg/Iz9aPz8NCg=

enter image description here

freak_geek
  • 109
  • 8

2 Answers2

1

get familiar with base64 encoding, especially read about padding, but in short: your "faked" String should probably ends with = or ==. you can't use regexp in here, as you should do some bitwise math, as every base64 digit needs 6 bits, in the meanwhile common usage (e.g. printing on screen) would use 8 bits per digit. you have to calculate/respect this padding. more info in THIS topic (read ALL answers and comments, accepted one isn't reliable!)

snachmsm
  • 17,866
  • 3
  • 32
  • 74
1

It matches platformAndroidSignatureHash because the last part with the equals signs is optional.

You could rewrite the pattern as

^(?=.{28}$)(?:[A-Za-z0-9+/]{4})+[A-Za-z0-9+/]{2,3}==?$

The pattern matches;

  • ^ Start of string
  • (?=.{28}$) Positive lookahead, assert 28 characters
  • (?:[A-Za-z0-9+/]{4})+ Repeat 1+ times 4 chars of the listed in the character class
  • [A-Za-z0-9+/]{2,3} Repeat 2-3 times matching one of the listed chars
  • ==? Match either = or ==
  • $ End of string

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70