func GetHash(a string) (hash.Hash, error) { var h hash.Hash switch a { case "adler32": h = adler32.New() case "crc32", "crc32ieee": h = crc32.New(crc32.MakeTable(crc32.IEEE)) case "crc32castagnoli": h = crc32.New(crc32.MakeTable(crc32.Castagnoli)) case "crc32koopman": h = crc32.New(crc32.MakeTable(crc32.Koopman)) case "crc64", "crc64iso": h = crc64.New(crc64.MakeTable(crc64.ISO)) case "crc64ecma": h = crc64.New(crc64.MakeTable(crc64.ECMA)) case "fnv", "fnv32": h = fnv.New32() case "fnv32a": h = fnv.New32a() case "fnv64": h = fnv.New64() case "fnv64a": h = fnv.New64a() case "hmac", "hmacsha256": h = hmac.New(sha256.New, []byte(key)) case "hmacmd5": h = hmac.New(md5.New, []byte(key)) case "hmacsha1": h = hmac.New(sha1.New, []byte(key)) case "hmacsha512": h = hmac.New(sha512.New, []byte(key)) case "md4": h = md4.New() case "md5": h = md5.New() case "ripemd160": h = ripemd160.New() case "sha1": h = sha1.New() case "sha224": h = sha256.New224() case "sha256": h = sha256.New() case "sha384": h = sha512.New384() case "sha512": h = sha512.New() default: return nil, errors.New("Invalid algorithm") } return h, nil }
func (s *descbc) Decrypt(salt []byte, algo, usage int, data []byte) ([]byte, error) { var h hash.Hash switch algo { case cryptDesCbcMd5: h = md5.New() case cryptDesCbcMd4: h = md4.New() default: return nil, ErrProtocol } if (len(data) & 7) != 0 { return nil, ErrProtocol } iv := [8]byte{} b, _ := des.NewCipher(s.key) c := cipher.NewCBCDecrypter(b, iv[:]) c.CryptBlocks(data, data) chk := make([]byte, h.Size()) h.Write(data[:8]) h.Write(chk) // Just need h.Size() zero bytes instead of the checksum h.Write(data[8+len(chk):]) h.Sum(chk[:0]) if subtle.ConstantTimeCompare(chk, data[8:8+len(chk)]) != 1 { return nil, ErrProtocol } return data[8+len(chk):], nil }
func calcNTResponse(nonce [8]byte, password string) [24]byte { var hash [21]byte h := md4.New() h.Write(appendUTF16LE(nil, password)) h.Sum(hash[:0]) return calcNTLMResponse(nonce, hash) }
func (s *descbc) Sign(algo, usage int, data ...[]byte) ([]byte, error) { var h hash.Hash switch algo { case signGssDes: sz := 0 for _, d := range data { sz += len(d) } sz = (sz + 7) &^ 7 u := make([]byte, sz) v := u[:0] for _, d := range data { v = append(v, d...) } iv := [8]byte{} b, _ := des.NewCipher(s.key) c := cipher.NewCBCEncrypter(b, iv[:]) c.CryptBlocks(u, u) return u[len(u)-8:], nil case signGssMd5Des: h = md5.New() for _, d := range data { h.Write(d) } return s.Sign(signGssDes, usage, h.Sum(nil)) case signMd5Des: h = md5.New() case signMd4Des: h = md4.New() default: return unkeyedSign(algo, usage, data...) } var key [8]byte for i := 0; i < 8; i++ { key[i] = s.key[i] ^ 0xF0 } chk := make([]byte, 24) io.ReadFull(rand.Reader, chk[:8]) h.Write(chk[:8]) for _, d := range data { h.Write(d) } h.Sum(chk[8:]) iv := [8]byte{} b, _ := des.NewCipher(s.key) c := cipher.NewCBCEncrypter(b, iv[:]) c.CryptBlocks(chk, chk) return chk, nil }
func TestRunReceiver(t *testing.T) { return var wg sync.WaitGroup c := md4.New() io.WriteString(c, "hello world") s := fmt.Sprintf("%x", c.Sum(nil)) fmt.Println(s) wg.Add(1) var srvCfg ReceiverConfig srvCfg.ListenAddr = "0.0.0.0:9999" srvCfg.SyncRootFolder = "/Users/smalllixin/Documents/lightchaser/temp" syncReceiver := NewSyncReceiver(&srvCfg) go func() { syncReceiver.Start() }() go func() { defer func() { fmt.Println("client defer run") syncReceiver.Stop() wg.Done() }() var clientCfg SenderConfig clientCfg.SrvAddr = "127.0.0.1:9999" clientCfg.SyncRootFolder = "/Users/smalllixin/Documents/pyspace" client := NewSyncSender(&clientCfg) files, err := ioutil.ReadDir(clientCfg.SyncRootFolder) if err != nil { t.Fatal("ReadDir err:", err) return } for _, f := range files { if !f.IsDir() { fileToSync := f.Name() //path.Join(clientCfg.SyncRootFolder, ) client.SyncAFile(fileToSync) } } //client.SyncAFile("udpclient.py") }() wg.Wait() //time.Sleep() }
// rc4HmacKey converts a UTF8 password into a key suitable for use with the // rc4hmac. func rc4HmacKey(password string) []byte { // Convert password from UTF8 to UTF16-LE s := make([]byte, 0) for _, r := range password { if r > 0x10000 { a, b := utf16.EncodeRune(r) s = append(s, byte(a), byte(a>>8), byte(b), byte(b>>8)) } else { s = append(s, byte(r), byte(r>>8)) } } h := md4.New() h.Write(s) return h.Sum(nil) }
func unkeyedSign(algo, usage int, data ...[]byte) ([]byte, error) { var h hash.Hash switch algo { case signMd5: h = md5.New() case signMd4: h = md4.New() default: return nil, ErrProtocol } for _, d := range data { h.Write(d) } return h.Sum(nil), nil }
func (s *descbc) Encrypt(salt []byte, usage int, data ...[]byte) []byte { var h hash.Hash switch s.etype { case cryptDesCbcMd5: h = md5.New() case cryptDesCbcMd4: h = md4.New() default: panic("") } outsz := 8 + h.Size() for _, d := range data { outsz += len(d) } outsz = (outsz + 7) &^ 7 out := make([]byte, outsz) io.ReadFull(rand.Reader, out[:8]) v := out[8+h.Size():] for _, d := range data { n := copy(v, d) v = v[n:] } h.Write(out) h.Sum(out[:8]) iv := [8]byte{} b, _ := des.NewCipher(s.key) c := cipher.NewCBCEncrypter(b, iv[:]) c.CryptBlocks(out, out) return out }
func GetHash(alg string) (hash.Hash, error) { var h hash.Hash switch alg { default: return nil, fmt.Errorf("Invalid algorithm") case "md4": h = md4.New() case "md5": h = md5.New() case "ripemd160": h = ripemd160.New() case "sha1": h = sha1.New() case "sha224": h = sha256.New224() case "sha256": h = sha256.New() case "sha384": h = sha512.New384() case "sha512": h = sha512.New() } return h, nil }
func main() { h := md4.New() h.Write([]byte("Rosetta Code")) fmt.Printf("%x\n", h.Sum(nil)) }
func md4Sum(data []byte, list []byte) []byte { md4 := md4.New() md4.Write(data) return md4.Sum(list) }