github.com/sagernet/sing-shadowsocks2@v0.2.0/internal/legacykey/key.go (about)

     1  package legacykey
     2  
     3  import (
     4  	"crypto/md5"
     5  	"crypto/sha1"
     6  	"io"
     7  
     8  	"github.com/sagernet/sing/common"
     9  
    10  	"golang.org/x/crypto/hkdf"
    11  )
    12  
    13  func Key(password []byte, keySize int) []byte {
    14  	var b, prev []byte
    15  	h := md5.New()
    16  	for len(b) < keySize {
    17  		h.Write(prev)
    18  		h.Write(password)
    19  		b = h.Sum(b)
    20  		prev = b[len(b)-h.Size():]
    21  		h.Reset()
    22  	}
    23  	return b[:keySize]
    24  }
    25  
    26  func Kdf(key, iv, out []byte) {
    27  	kdf := hkdf.New(sha1.New, key, iv, []byte("ss-subkey"))
    28  	common.Must1(io.ReadFull(kdf, out))
    29  }