What is the math behind a private key BASE58 (P2PKH)

Using this link I can understand something about a private key BASE58 (P2PKH):

“such encodings will always yield a 51-character string that starts with ‘5’, or more specifically, either ‘5H’, ‘5J’, or ‘5K’.”

So let’s say I have this model of private key:


I know that is not a valid private key because I can use a website such as bitaddress to tell me about this.

Taking a private key like this, I would like to understand how I can verify if it is valid, what the procedures are, because I would like to write a script to play around with this.

Keep in mind I don’t have any other information, just a string like that.