Skip to content

Instantly share code, notes, and snippets.

@Meroje
Created June 6, 2025 09:12
Show Gist options
  • Save Meroje/89c820e4e8b7ef513c44e2f707b9b375 to your computer and use it in GitHub Desktop.
Save Meroje/89c820e4e8b7ef513c44e2f707b9b375 to your computer and use it in GitHub Desktop.
import base64
import binascii
def AWSAccount_from_AWSKeyID(AWSKeyID):
trimmed_AWSKeyID = AWSKeyID[4:] #remove KeyID prefix
x = base64.b32decode(trimmed_AWSKeyID) #base32 decode
y = x[0:6]
z = int.from_bytes(y, byteorder='big', signed=False)
mask = int.from_bytes(binascii.unhexlify(b'7fffffffff80'), byteorder='big', signed=False)
e = (z & mask)>>7
return (e)
print ("account id:" + "{:012d}".format(AWSAccount_from_AWSKeyID("ASIAQNZGKIQY56JQ7WML")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment