Created
July 28, 2016 06:39
-
-
Save ruandao/cafb2819d7ffa40accc3ccc81c5d7e9f to your computer and use it in GitHub Desktop.
<<Network programming with Go>>
Security
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/crypto/blowfish" | |
"fmt" | |
"bytes" | |
) | |
func main_Blowfish() { | |
key := []byte("my key") | |
cipher, err := blowfish.NewCipher(key) | |
if hasErr(err) { | |
fmt.Println(err.Error()) | |
} | |
src := []byte("hello\n\n\n") | |
var enc [512]byte | |
cipher.Encrypt(enc[0:], src) | |
var descrypt [8]byte | |
cipher.Decrypt(descrypt[0:], enc[0:]) | |
result := bytes.NewBuffer(nil) | |
result.Write(descrypt[0:8]) | |
fmt.Println(string(result.Bytes())) | |
} | |
func hasErr(err error) bool { | |
return err != nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/md5" | |
"fmt" | |
) | |
func main_MD5Hash() { | |
hash := md5.New() | |
bytes := []byte("hello\n") | |
hash.Write(bytes) | |
hashValue := hash.Sum(nil) | |
hashSize := hash.Size() | |
for n := 0; n < hashSize; n += 4 { | |
var val uint32 | |
val = uint32(hashValue[n]) << 24 + | |
uint32(hashValue[n+1]) << 16 + | |
uint32(hashValue[n+2]) << 8 + | |
uint32(hashValue[n+3]) | |
fmt.Printf("%x ", val) | |
} | |
fmt.Println() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<module type="GO_MODULE" version="4"> | |
<component name="NewModuleRootManager" inherit-compiler-output="true"> | |
<exclude-output /> | |
<content url="file://$MODULE_DIR$" /> | |
<orderEntry type="inheritedJdk" /> | |
<orderEntry type="sourceFolder" forTests="false" /> | |
<orderEntry type="library" name="GOPATH <Security>" level="project" /> | |
</component> | |
</module> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment