This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Hikvision API
#1
Hi, 

I need to communicate with Hikvision devices via API.
The devices, however, use "digest" authentication, not basic

I tried this code,but  I got 401....


Code:
local http = require('socket.http') local ltn12 = require('ltn12') -- CONFIGURAZIONE local ip = "192.168.0.245" local user = "admin" local pass = "TuaPassword" -- <--- Inserisci la tua password local url = "/ISAPI/System/deviceInfo" -- FUNZIONE MD5 PURE LUA (Nessuna libreria richiesta) local function md5_calc(message)     local function str2hex(s) return (s:gsub('.', function(c) return string.format('%02x', string.byte(c)) end)) end     local function word2str(w)         local s = ""         for i = 1, 4 do s = s .. string.char(bit.band(bit.rshift(w, (i - 1) * 8), 0xff)) end         return s     end     -- Logic Machine ha la libreria bit integrata, usiamo quella     return string.md5(message) end -- Se string.md5 fallisce di nuovo, usiamo questa scorciatoia che LM usa internamente function calculate_md5(str)     -- Proviamo il metodo più diretto possibile per LM 2025     local ok, res = pcall(function() return crypto.hash(str, 'md5') end)     if ok then return res end         ok, res = pcall(function() return string.md5(str) end)     if ok then return res end     ok, res = pcall(function() return encodetools.md5(str) end)     if ok then return res end         error("Impossibile trovare una funzione MD5 valida") end function get_hikvision_info_digest()     local response_body = {}         -- 1. Prima chiamata per ottenere la sfida     local res, code, headers = http.request("http://" .. ip .. url)         if code == 401 and headers['www-authenticate'] then         local auth_header = headers['www-authenticate']         local realm = auth_header:match('realm="([^"]+)"')         local nonce = auth_header:match('nonce="([^"]+)"')         local qop = auth_header:match('qop="([^"]+)"')                 local method = "GET"         local uri = url         local nc = "00000001"         local cnonce = string.format("%08x", os.time())                 -- Calcolo Digest         local HA1 = calculate_md5(user .. ":" .. realm .. ":" .. pass)         local HA2 = calculate_md5(method .. ":" .. uri)         local response = calculate_md5(HA1 .. ":" .. nonce .. ":" .. nc .. ":" .. cnonce .. ":" .. qop .. ":" .. HA2)                 local digest_header = string.format(             'Digest username="%s", realm="%s", nonce="%s", uri="%s", response="%s", qop=%s, nc=%s, cnonce="%s"',             user, realm, nonce, uri, response, qop, nc, cnonce         )                 -- 2. Seconda chiamata         res, code, headers = http.request({             url = "http://" .. ip .. url,             method = "GET",             headers = { ["Authorization"] = digest_header },             sink = ltn12.sink.table(response_body)         })                 local response_str = table.concat(response_body)         log("Codice: " .. tostring(code))         if code == 200 then             log("SUCCESSO! Dati dispositivo:")             log(response_str)         else             log("Errore: " .. response_str)         end     else         log("Dispositivo non risponde o non richiede Auth. Codice: " .. tostring(code))     end end get_hikvision_info_digest()
.
Is it possible to implement this feature in LM?
Reply
#2
Try code from this post: https://forum.logicmachine.net/showthrea...8#pid40628

Your code won't even work on LM because there are no such libraries that calculate_md5() tries to use.
Reply


Forum Jump: