This repository was archived by the owner on Jun 21, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +63
-0
lines changed Expand file tree Collapse file tree 1 file changed +63
-0
lines changed Original file line number Diff line number Diff line change
1
+ package digest_auth_client
2
+
3
+ import (
4
+ "crypto/md5"
5
+ "crypto/sha256"
6
+ "fmt"
7
+ "hash"
8
+ "io"
9
+ "strings"
10
+ )
11
+
12
+ type AuthorizationHeader struct {
13
+ Algorithm string // unquoted
14
+ Body string // request value
15
+ Cnonce string // quoted
16
+ Method string // request value
17
+ Nc string // unquoted
18
+ Opaque string // quoted
19
+ Qop string // unquoted
20
+ Realm string // quoted
21
+ Resposne string // quoted
22
+ Uri string // quoted
23
+ Userhash string // quoted
24
+ Username string // quoted
25
+ Username_ string // quoted
26
+ }
27
+
28
+ func (ah * AuthorizationHeader ) ComputeResponse () AuthorizationHeader {
29
+ return * ah
30
+ }
31
+
32
+ func (ah * AuthorizationHeader ) ComputeA1 () AuthorizationHeader {
33
+ return * ah
34
+ }
35
+
36
+ func (ah * AuthorizationHeader ) ComputeA2 () (s string ) {
37
+
38
+ if strings .Compare (ah .Qop , "auth" ) == 0 || strings .Compare (ah .Qop , "" ) == 0 {
39
+ s = fmt .Sprintf ("%s:%s" , ah .Method , ah .Uri )
40
+ }
41
+
42
+ if strings .Compare (ah .Qop , "auth-int" ) == 0 {
43
+ s = fmt .Sprintf ("%s:%s" , s , ah .Hash (ah .Body ))
44
+ }
45
+
46
+ return
47
+ }
48
+
49
+ func (ah * AuthorizationHeader ) Hash (a string ) (s string ) {
50
+
51
+ var h hash.Hash
52
+
53
+ if strings .Compare (ah .Algorithm , "MD5" ) == 0 {
54
+ h = md5 .New ()
55
+ } else if strings .Compare (ah .Algorithm , "SHA-256" ) == 0 {
56
+ h = sha256 .New ()
57
+ }
58
+
59
+ io .WriteString (h , a )
60
+ s = string (h .Sum (nil ))
61
+
62
+ return
63
+ }
You can’t perform that action at this time.
0 commit comments