Skip to content

Instantly share code, notes, and snippets.

@62mkv
Created October 11, 2016 05:01
Show Gist options
  • Select an option

  • Save 62mkv/1e04a7c13b0bfe99ae7280adca3e494d to your computer and use it in GitHub Desktop.

Select an option

Save 62mkv/1e04a7c13b0bfe99ae7280adca3e494d to your computer and use it in GitHub Desktop.
Python script to calculate SIP REGISTER Digest authentication (in order to check password validity)
from md5 import md5
# header from 401 response on REGISTER
# Authorization: Digest username="883140776410950", realm="gw_youmagic", algorithm=MD5, uri="sip:GW_Youmagic", nonce="1476157437:0a1418a40f8ee1c9a55f1587ab931c14", response="3ff479ccc24874f66aae45dac889d099"
login = '883140776410950'
uri = 'sip:GW_Youmagic'
nonce = '1476157437:0a1418a40f8ee1c9a55f1587ab931c14'
realm = 'gw_youmagic'
password = '----------'
str1 = md5("{}:{}:{}".format(login,realm,password)).hexdigest()
str2 = md5("REGISTER:{}".format(uri)).hexdigest()
str3 = md5("{}:{}:{}".format(str1,nonce,str2)).hexdigest()
print str3
@gil-obradors

Copy link
Copy Markdown
#Upgraded python 3.6
from hashlib import md5

# header from 401 response on REGISTER
# Authorization: Digest username="883140776410950", realm="gw_youmagic", algorithm=MD5, uri="sip:GW_Youmagic", nonce="1476157437:0a1418a40f8ee1c9a55f1587ab931c14", response="3ff479ccc24874f66aae45dac889d099"

login = str('883140776410950')
uri = str('sip:GW_Youmagic')
nonce = str('1476157437:0a1418a40f8ee1c9a55f1587ab931c14')
realm = str('gw_youmagic')
password = str('----------')

str1 = md5("{}:{}:{}".format(login,realm,password).encode('utf-8')).hexdigest()
str2 = md5("REGISTER:{}".format(uri).encode('utf-8')).hexdigest()
str3 = md5("{}:{}:{}".format(str1,nonce,str2).encode('utf-8')).hexdigest()
print(str3)

@beneditomarques

Copy link
Copy Markdown

How can I calculate this, when the authenticate header comes with "qop", "cnonce", "opaque" and "nc" parameters? applying this scripts in this kind of headers, the response is not correct.

@62mkv

62mkv commented Dec 10, 2021

Copy link
Copy Markdown
Author

I have no idea :) this is something I've put here for myself as a quick hack to validate signature and probably only ever used once. feel free to expand as you wish

@brianwyld

brianwyld commented Jul 22, 2026

Copy link
Copy Markdown

Thanks for that : allowed me to validate my sip client code (C on zephyr on nrf5340) was doing the right thing.
--- version with qop, cnonce etc as per RFC 2617

#Upgraded python 3.6
from hashlib import md5

# header from 401 response on REGISTER eg
# tsip: (OK)
# WWW-Authenticate: Digest realm="asterisk",nonce="1784720550/7b47bb9c7820fb1b9606983877a83d33",opaque="6005583754bba4c5",stale=true,algorithm=MD5,qop="auth"
# Authorization: Digest username="6003", realm="asterisk", nonce="1784720550/7b47bb9c7820fb1b9606983877a83d33", uri="sip:192.168.1.20:5060;transport=udp", response="bcde941ad2ecccbb77ad1681fc5a7afb", opaque="6005583754bba4c5", cnonce="5ad246ab0b8f6a40", qop=auth, nc=00000001
# cc2 : 
# *I020.265:netsip:auth:user[38b8ebe1006e] pass[toto] realm[asterisk] uri[sip:infrafon.call] qop[auth] nc[00000001]
# *I020.275:netsip:auth:nonce[1784723668/22a86cbfe948f6dee4d559ae3e7f2f3f] cnonce[38b8ebe1006e-00004f28]
# *I020.285:netsip:auth:response[d6bc0220e52c50a601490c9a5a27f932]

login = str('38b8ebe1006e')
realm = str('asterisk')
password = str('toto')
uri = str('sip:infrafon.call')
nonce = str('1784723668/22a86cbfe948f6dee4d559ae3e7f2f3f')
cnonce = str('38b8ebe1006e-00004f28')
qop = str('auth')
nc = str('00000001')

str1 = md5("{}:{}:{}".format(login,realm,password).encode('utf-8')).hexdigest()
str2 = md5("REGISTER:{}".format(uri).encode('utf-8')).hexdigest()
str3 = md5("{}:{}:{}:{}:{}:{}".format(str1,nonce,nc,cnonce,qop,str2).encode('utf-8')).hexdigest()
print(str3)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment