Skip to content

Commit bfecdb0

Browse files
authored
Update DigestAuthorization.ino (Simple example update) (#7579)
Simple example update to pass the method as a parameter to getDigestAuth(), so it is more easily used for POST. Add setting the ransom seed to RANDOM_REG32 in setup() for better getCNonce() values.
1 parent 2171a2e commit bfecdb0

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino

+4-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ String getCNonce(const int len) {
4747
return s;
4848
}
4949

50-
String getDigestAuth(String& authReq, const String& username, const String& password, const String& uri, unsigned int counter) {
50+
String getDigestAuth(String& authReq, const String& username, const String& password, const String& method, const String& uri, unsigned int counter) {
5151
// extracting required parameters for RFC 2069 simpler Digest
5252
String realm = exractParam(authReq, "realm=\"", '"');
5353
String nonce = exractParam(authReq, "nonce=\"", '"');
@@ -64,7 +64,7 @@ String getDigestAuth(String& authReq, const String& username, const String& pass
6464
String h1 = md5.toString();
6565

6666
md5.begin();
67-
md5.add(String("GET:") + uri);
67+
md5.add(method + ":" + uri);
6868
md5.calculate();
6969
String h2 = md5.toString();
7070

@@ -81,6 +81,7 @@ String getDigestAuth(String& authReq, const String& username, const String& pass
8181
}
8282

8383
void setup() {
84+
randomSeed(RANDOM_REG32);
8485
Serial.begin(115200);
8586

8687
WiFi.mode(WIFI_STA);
@@ -118,7 +119,7 @@ void loop() {
118119
String authReq = http.header("WWW-Authenticate");
119120
Serial.println(authReq);
120121

121-
String authorization = getDigestAuth(authReq, String(username), String(password), String(uri), 1);
122+
String authorization = getDigestAuth(authReq, String(username), String(password), "GET", String(uri), 1);
122123

123124
http.end();
124125
http.begin(client, String(server) + String(uri));

0 commit comments

Comments
 (0)