-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcram.go
39 lines (32 loc) · 939 Bytes
/
cram.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright 2014, 2021 Tamás Gulácsi. All rights reserved.
//
// SPDX-License-Identifier: Apache-2.0
package imapclient
import (
"crypto/hmac"
"crypto/md5" //nolint:gas
"encoding/hex"
"github.com/emersion/go-sasl"
)
type cramAuth struct {
username, password string
}
// CramAuth returns an sasl.Client usable for CRAM-MD5 authentication.
func CramAuth(username, password string) sasl.Client {
return cramAuth{username: username, password: password}
}
func (a cramAuth) Start() (mech string, ir []byte, err error) {
return "CRAM-MD5", ir, nil
}
func (a cramAuth) Next(challenge []byte) (response []byte, err error) {
h := hmac.New(md5.New, []byte(a.password))
h.Write(challenge)
n := len(a.username)
response = make([]byte, 0, len(a.username)+1+hex.EncodedLen(h.Size()))
for i := 0; i < n; i++ {
response[i] = byte(a.username[i])
}
response[n] = ' '
hex.Encode(response[n+1:], h.Sum(nil))
return response, nil
}