github.com/unclejack/drone@v0.2.1-0.20140918182345-831b034aa33b/pkg/model/util.go (about)

     1  package model
     2  
     3  import (
     4  	"crypto/md5"
     5  	"crypto/rand"
     6  	"crypto/rsa"
     7  	"crypto/x509"
     8  	"encoding/pem"
     9  	"fmt"
    10  	"strings"
    11  	"unicode"
    12  
    13  	"code.google.com/p/go.crypto/ssh"
    14  	"code.google.com/p/go.text/unicode/norm"
    15  	"github.com/dchest/uniuri"
    16  )
    17  
    18  var (
    19  	lat = []*unicode.RangeTable{unicode.Letter, unicode.Number}
    20  	nop = []*unicode.RangeTable{unicode.Mark, unicode.Sk, unicode.Lm}
    21  )
    22  
    23  // helper function to create a Gravatar Hash
    24  // for the given Email address.
    25  func createGravatar(email string) string {
    26  	email = strings.ToLower(strings.TrimSpace(email))
    27  	hash := md5.New()
    28  	hash.Write([]byte(email))
    29  	return fmt.Sprintf("%x", hash.Sum(nil))
    30  }
    31  
    32  // helper function to create a Slug for the
    33  // given string of text.
    34  func createSlug(s string) string {
    35  	buf := make([]rune, 0, len(s))
    36  	dash := false
    37  	for _, r := range norm.NFKD.String(s) {
    38  		switch {
    39  		// unicode 'letters' like mandarin characters pass through
    40  		case unicode.IsOneOf(lat, r):
    41  			buf = append(buf, unicode.ToLower(r))
    42  			dash = true
    43  		case unicode.IsOneOf(nop, r):
    44  			// skip
    45  		case dash:
    46  			buf = append(buf, '-')
    47  			dash = false
    48  		}
    49  	}
    50  	if i := len(buf) - 1; i >= 0 && buf[i] == '-' {
    51  		buf = buf[:i]
    52  	}
    53  	return string(buf)
    54  }
    55  
    56  // helper function to create a random 40-byte
    57  // Token that is URL-friendly.
    58  func createToken() string {
    59  	return uniuri.NewLen(40)
    60  }
    61  
    62  // -----------------------------------------------------------------------------
    63  // SSH Functions
    64  
    65  const (
    66  	RSA_BITS     = 2048 // Default number of bits in an RSA key
    67  	RSA_BITS_MIN = 768  // Minimum number of bits in an RSA key
    68  )
    69  
    70  // helper function to generate an RSA Private Key.
    71  func generatePrivateKey() (*rsa.PrivateKey, error) {
    72  	return rsa.GenerateKey(rand.Reader, RSA_BITS)
    73  }
    74  
    75  // helper function that marshalls an RSA Public Key to an SSH
    76  // .authorized_keys format
    77  func marshalPublicKey(pubkey *rsa.PublicKey) string {
    78  	pk, err := ssh.NewPublicKey(pubkey)
    79  	if err != nil {
    80  		return ""
    81  	}
    82  
    83  	return string(ssh.MarshalAuthorizedKey(pk))
    84  }
    85  
    86  // helper function that marshalls an RSA Private Key to
    87  // a PEM encoded file.
    88  func marshalPrivateKey(privkey *rsa.PrivateKey) string {
    89  	privateKeyMarshaled := x509.MarshalPKCS1PrivateKey(privkey)
    90  	privateKeyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Headers: nil, Bytes: privateKeyMarshaled})
    91  	return string(privateKeyPEM)
    92  }