Secure Remote Password protocol

The Secure Remote Password protocol (SRP) is an augmented password-authenticated key exchange (PAKE) protocol, specifically designed to work around existing patents.
Like all PAKE protocols, an eavesdropper or man in the middle cannot obtain enough information to be able to brute-force guess a password or apply a dictionary attack without further interactions with the parties for each guess. Furthermore, being an augmented PAKE protocol, the server does not store password-equivalent data. This means that an attacker who steals the server data cannot masquerade as the client unless they first perform a brute force search for the password.
In layman's terms, during SRP (or any other PAKE protocol) authentication, one party (the "client" or "user") demonstrates to another party (the "server") that they know the password, without sending the password itself nor any other information from which the password can be derived. The password never leaves the client and is unknown to the server.
Furthermore, the server also needs to know about the password (but not the password itself) in order to instigate the secure connection. This means that the server also authenticates itself to the client which prevents phishing without reliance on the user parsing complex URLs.
A newer alternative algorithm is OPAQUE
Overview.
The SRP protocol has a number of desirable properties: it allows a user to authenticate themselves to a server, it is resistant to dictionary attacks mounted by an eavesdropper, and it does not require a trusted third party. It effectively conveys a zero-knowledge password proof from the user to the server. In revision 6 of the protocol only one password can be guessed per connection attempt. One of the interesting properties of the protocol is that even if one or two of the cryptographic primitives it uses are attacked, it is still secure. The SRP protocol has been revised several times, and is currently at revision 6a.
The SRP protocol creates a large private key shared between the two parties in a manner similar to Diffie–Hellman key exchange based on the client side having the user password and the server side having a cryptographic verifier derived from the password. The shared public key is derived from two random numbers, one generated by the client, and the other generated by the server, which are unique to the login attempt. In cases where encrypted communications as well as authentication are required, the SRP protocol is more secure than the alternative SSH protocol and faster than using Diffie–Hellman key exchange with signed messages. It is also independent of third parties, unlike Kerberos. The SRP protocol, version 3 is described in RFC 2945. SRP version 6 is also used for strong password authentication in SSL/TLS (in TLS-SRP) and other standards such as EAP and SAML, and is being standardized in IEEE P1363 and ISO/IEC 11770-4.
Protocol.
The following notation is used in this description of the protocol, version 6:
All other variables are defined in terms of these.
First, to establish a password "p" with server Steve, client Carol picks a small random salt "s", and computes "x" = "H"("s", "p"), "v" = "g". Steve stores "v" and "s", indexed by , as Carol's password verifier and salt. Carol must not share "x" with anybody, and must safely erase it at this step, because it is equivalent to the plaintext password "p". This step is completed before the system is used as part of the user registration with Steve. Note that the salt "s" is shared and exchanged to negotiate a session key later so the value could be chosen by either side but is done by Carol so that she can register , "s" and "v" in a single registration request. The transmission and authentication of the registration request is not covered in SRP.
Then to perform a proof of password at a later date the following exchange protocol occurs:
Now the two parties have a shared, strong session key "K". To complete authentication, they need to prove to each other that their keys match. One possible way is as follows:
This method requires guessing more of the shared state to be successful in impersonation than just the key. While most of the additional state is public, private information could safely be added to the inputs to the hash function, like the server private key.
Alternatively, in a password-only proof the calculation of "K" can be skipped and the shared "S" proven with:
When using SRP to negotiate a shared key "K" which will be immediately used after the negotiation the verification steps of "M"1 and "M"2 may be skipped. The server will reject the very first request from the client which it cannot decrypt. Skipping the verification steps can be dangerous. 
The two parties also employ the following safeguards:
Example code in Python.
An example SRP authentication
WARNING: Do not use for real cryptographic purposes beyond testing.
WARNING: This below code misses important safeguards. It does not check A, B, and U are not zero.
based on http://srp.stanford.edu/design.html
import hashlib
import random
def H(*args) -> int:
 """A one-way hash function."""
 a = ":".join(str(a) for a in args)
 return int(hashlib.sha256(a.encode("utf-8")).hexdigest(), 16)
def cryptrand(n: int = 1024):
 return random.SystemRandom().getrandbits(n) % N
N = """00:c0:37:c3:75:88:b4:32:98:87:e6:1c:2d:a3:32:
 4b:1b:a4:b8:1a:63:f9:74:8f:ed:2d:8a:41:0c:2f:
 c2:1b:12:32:f0:d3:bf:a0:24:27:6c:fd:88:44:81:
 97:aa:e4:86:a6:3b:fc:a7:b8:bf:77:54:df:b3:27:
 c7:20:1f:6f:d1:7f:d7:fd:74:15:8b:d3:1c:e7:72:
 c9:f5:f8:ab:58:45:48:a9:9a:75:9b:5a:2c:05:32:
 16:2b:7b:62:18:e8:f1:42:bc:e2:c3:0d:77:84:68:
 9a:48:3e:09:5e:70:16:18:43:79:13:a8:c3:9c:3d:
 d0:d4:ca:3c:50:0b:88:5f:e3"""
N = int("".join(N.split()).replace(":", ""), 16)
g = 2 # A generator modulo N
k = H(N, g) # Multiplier parameter (k=3 in legacy SRP-6)
F = '#0x' # Format specifier
print("#. H, N, g, and k are known beforehand to both client and server:")
print(f'{H = }\n{N = :{F}}\n{g = :{F}}\n{k = :{F}}')
print("\n0. server stores (I, s, v) in its password database")
I = "person" # Username
p = "password1234" # Password
s = cryptrand(64) # Salt for the user
x = H(s, I, p) # Private key
v = pow(g, x, N) # Password verifier
print(f'{I = }\n{p = }\n{s = :{F}}\n{x = :{F}}\n{v = :{F}}')
print("\n1. client sends username I and public ephemeral value A to the server")
a = cryptrand()
A = pow(g, a, N)
print(f"{I = }\n{A = :{F}}") # client->server (I, A)
print("\n2. server sends user's salt s and public ephemeral value B to client")
b = cryptrand()
B = (k * v + pow(g, b, N)) % N
print(f"{s = :{F}}\n{B = :{F}}") # server->client (s, B)
print("\n3. client and server calculate the random scrambling parameter")
u = H(A, B) # Random scrambling parameter
print(f"{u = :{F}}")
print("\n4. client computes session key")
x = H(s, I, p)
S_c = pow(B - k * pow(g, x, N), a + u * x, N)
K_c = H(S_c)
print(f"{S_c = :{F}}\n{K_c = :{F}}")
print("\n5. server computes session key")
S_s = pow(A * pow(v, u, N), b, N)
K_s = H(S_s)
print(f"{S_s = :{F}}\n{K_s = :{F}}")
print("\n6. client sends proof of session key to server")
M_c = H(H(N) ^ H(g), H(I), s, A, B, K_c)
print(f"{M_c = :{F}}")
print("\n7. server sends proof of session key to client")
M_s = H(A, M_c, K_s)
print(f"{M_s = :{F}}")