Go

Min/max integer values

1
2
3
4
const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(^uint(0) >> 1)
const MinInt = -MaxInt - 1

[https://groups.google.com/group/golang-nuts/msg/71c307e4d73024ce?pli=1]

Reduce binary size by stripping debugging symbols

1
go build -ldflags '-w -s' main.go

Disable optimizations and inlining

1
go run -gcflags '-N -l' main.go

compile - The Go Programming Language

Calculate sha256

1
2
3
h := sha256.New()
h.Write(b)
sum := h.Sum(nil)

Compile to ARM with CGO

Install support programs

1
2
sudo apt-get install libc6-armel-cross libc6-dev-armel-cross
sudo apt-get install binutils-arm-linux-gnueabi

Install cross compilers for arm

1
2
sudo apt-get install gcc-arm-linux-gnueabi
sudo apt-get install g++-arm-linux-gnueabi

Install cross compilers for armhf

1
2
sudo apt-get install gcc-arm-linux-gnueabihf
sudo apt-get install g++-arm-linux-gnueabihf

Compile package

1
env GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=1 CC=arm-linux-gnueabi-gcc go build .

CGO

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
package main

// #include <stdio.h>
//
// int hello() {
//   printf("hello world!\n");
// }
import "C"

func main() {
	C.hello()
}

cgo - The Go Programming Language

Go for Crypto Developers

Encryption

AES

Hashes

SHA2

Signatures

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
// generate key
key, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)

// sign message
digest := sha256.Sum256(data)
r, s, _ := ecdsa.Sign(rand.Reader, priv, digest[:])
// encode the signature
params := priv.Curve.Params()
curveByteSize := params.P.BitLen() / 8
rBytes, sBytes := r.Bytes(), s.Bytes()
signature := make([]byte, curveByteSize*2)
copy(signature[curveByteSize-len(rBytes):], rBytes)
copy(signature[curveByteSize*2-len(sBytes):], sBytes)

// verify
digest := sha256.Sum256(data)
curveByteSize := pub.Curve.Params().P.BitLen() / 8
r, s := new(big.Int), new(big.Int)
r.SetBytes(signature[:curveByteSize])
s.SetBytes(signature[curveByteSize:])
ecdsa.Verify(pub, digest[:], r, s)

Using Papertrail with logrus

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import "github.com/polds/logrus-papertrail-hook"

// ...

hook, err := papertrail.NewPapertrailHook(&papertrail.Hook{
  Host:     "logs3.papertrailapp.com",
  Port:     13296,
  Hostname: "hostname",
  Appname:  "appname",
})
if err != nil {
  logrus.Printf("cannot create papertrail hook, err: %v", err)
  return
}
hook.SetLevels(logrus.AllLevels)
logger.AddHook(hook)