daily pastebin goal
35%
SHARE
TWEET

Untitled

a guest Jun 19th, 2017 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package util
  2.  
  3. import (
  4.     "fmt"
  5.     "math/big"
  6.     "crypto/dsa"
  7.     "encoding/asn1"
  8.     "encoding/pem"
  9.     "crypto/rand"
  10.     "encoding/base64"
  11.     "crypto/sha1"
  12.     "bytes"
  13. )
  14.  
  15. var privateKeyDer = []byte(`-----BEGIN DSA PRIVATE KEY-----
  16. MIIBugIBAAKBgQCKBzr2/ImJNThK0ZrknfaqtObYC8ydpto8sZxxw8BWmUlyFApj
  17. OyVFQjdZMKp9dG5NelAv+XkS+03D49Bnzbc2TT10mPjbGzjhye4hIFJpPB61Zicg
  18. IDlgJq5GB9GwxH8fEeYfzEaO7igEfkS0xHsAubm7hCML4Wbd6Fk28NuhqQIVANRR
  19. 6z8MpieRWDR+AhS7CMjxuCJvAoGATJFGrtkHbfy1bXLs1/+z/ejlRWkFnx9Hok6e
  20. cXCO1lvIatkJsF8IdlbjYje8TMg2YV5Js31bsv7l1Lr9g5g8FWc938FjCxpF8mQ+
  21. TfSsNyzQhbFSAXopSZg8Zq5w3pxqwvS2e3MRzBTCvf7JL4ixQQJOzZSoJTiXUB1W
  22. 8l6vwq0CgYA7ubVqw68ojJ7UWdvtSfFJbN4YOoU0NF3XmWjZhFJAFEbkExt6ued/
  23. z7wbMp/l9Dmx41Of6f7El2pTSAWdYx2ARaNZoIpegPBkpN2gq1jZAMDUpx9oPklV
  24. 8Dvft4B+Kd74aCytE+r0V9nvRhEj7m6UHVj9lXtKhE0wVuWM4Dgt6AIUXKeEnhac
  25. F6s91KUB/iAuMjAIehc=
  26. -----END DSA PRIVATE KEY-----`)
  27.  
  28. var publicKeyDer = []byte(`-----BEGIN PUBLIC KEY-----
  29. MIIBtjCCASsGByqGSM44BAEwggEeAoGBAIoHOvb8iYk1OErRmuSd9qq05tgLzJ2m
  30. 2jyxnHHDwFaZSXIUCmM7JUVCN1kwqn10bk16UC/5eRL7TcPj0GfNtzZNPXSY+Nsb
  31. OOHJ7iEgUmk8HrVmJyAgOWAmrkYH0bDEfx8R5h/MRo7uKAR+RLTEewC5ubuEIwvh
  32. Zt3oWTbw26GpAhUA1FHrPwymJ5FYNH4CFLsIyPG4Im8CgYBMkUau2Qdt/LVtcuzX
  33. /7P96OVFaQWfH0eiTp5xcI7WW8hq2QmwXwh2VuNiN7xMyDZhXkmzfVuy/uXUuv2D
  34. mDwVZz3fwWMLGkXyZD5N9Kw3LNCFsVIBeilJmDxmrnDenGrC9LZ7cxHMFMK9/skv
  35. iLFBAk7NlKglOJdQHVbyXq/CrQOBhAACgYA7ubVqw68ojJ7UWdvtSfFJbN4YOoU0
  36. NF3XmWjZhFJAFEbkExt6ued/z7wbMp/l9Dmx41Of6f7El2pTSAWdYx2ARaNZoIpe
  37. gPBkpN2gq1jZAMDUpx9oPklV8Dvft4B+Kd74aCytE+r0V9nvRhEj7m6UHVj9lXtK
  38. hE0wVuWM4Dgt6A==
  39. -----END PUBLIC KEY-----`)
  40.  
  41. var privateKey *dsa.PrivateKey
  42. var publicKey *dsa.PublicKey
  43.  
  44. var println = fmt.Println
  45.  
  46. func init() {
  47.  
  48.     block, _ := pem.Decode(privateKeyDer)
  49.  
  50.     // initialize keypair
  51.     privateKey, _ = ParsePKCS8PrivateKey(block.Bytes)
  52.     publicKey = &privateKey.PublicKey
  53.  
  54.     println(privateKey)
  55.     println(publicKey)
  56. }
  57.  
  58. func Sign(data []byte) (signature string) {
  59.     _, _, signatureByte := sign(data, privateKey)
  60.  
  61.     return base64.StdEncoding.EncodeToString(signatureByte)
  62. }
  63.  
  64. func putLength(buf *bytes.Buffer, len int)  {
  65.     if len < 128 {
  66.         buf.WriteByte(byte(len))
  67.     } else if len < 256 {
  68.         buf.WriteRune(rune(-127))
  69.         buf.WriteByte(byte(len))
  70.     } else if len < 65536 {
  71.         buf.WriteRune(rune(-126))
  72.         buf.WriteByte(byte(len >> 8))
  73.         buf.WriteByte(byte(len))
  74.     } else if len < 16777216 {
  75.         buf.WriteRune(rune(-125))
  76.         buf.WriteByte(byte(len >> 16))
  77.         buf.WriteByte(byte(len >> 8))
  78.         buf.WriteByte(byte(len))
  79.     } else {
  80.         buf.WriteRune(rune(-124))
  81.         buf.WriteByte(byte(len >> 24))
  82.         buf.WriteByte(byte(len >> 16))
  83.         buf.WriteByte(byte(len >> 8))
  84.         buf.WriteByte(byte(len))
  85.     }
  86. }
  87.  
  88.  
  89. // golang cannot parse PKCS8 DSA private key right now
  90. // we need to make an struct for unmarshal
  91. type PKCS8DSAPrivateKey struct {
  92.     E1, P, Q, G, Y, X *big.Int
  93. }
  94.  
  95. func ParsePKCS8PrivateKey(der []byte) (privateKey *dsa.PrivateKey, err error) {
  96.  
  97.     myPrivateKey := PKCS8DSAPrivateKey{}
  98.  
  99.     asn1.Unmarshal(der, &myPrivateKey)
  100.  
  101.     privateKey = new(dsa.PrivateKey)
  102.  
  103.     dsaPublicKey := new(dsa.PublicKey)
  104.     dsaParameter := new(dsa.Parameters)
  105.  
  106.     dsaParameter.G = myPrivateKey.G
  107.     dsaParameter.P = myPrivateKey.P
  108.     dsaParameter.Q = myPrivateKey.Q
  109.  
  110.     dsaPublicKey.Y = myPrivateKey.Y
  111.     dsaPublicKey.Parameters = *dsaParameter
  112.  
  113.     privateKey.X = myPrivateKey.X
  114.     privateKey.PublicKey = *dsaPublicKey
  115.  
  116.     return
  117. }
  118.  
  119. func sign(data []byte, privatekey *dsa.PrivateKey) (r *big.Int, s *big.Int, signstr []byte) {
  120.  
  121.     sha1 := sha1.New()
  122.     sha1.Write(data)
  123.     signhash := sha1.Sum(nil)
  124.  
  125.     r, s, err := dsa.Sign(rand.Reader, privatekey, signhash[:])
  126.     if err != nil {
  127.         fmt.Println(err)
  128.     }
  129.  
  130.     der := genDerValueFromRS(r, s)
  131.  
  132.     return r, s, der
  133. }
  134.  
  135. func genDerValueFromRS(r, s *big.Int) (derValue []byte) {
  136.     rsBuf := bytes.NewBuffer(make([]byte, 0))
  137.  
  138.     rsBuf.WriteRune(2)
  139.     putLength(rsBuf, len(r.Bytes()))
  140.     rsBuf.Write(r.Bytes())
  141.  
  142.     rsBuf.WriteRune(2)
  143.     putLength(rsBuf, len(s.Bytes()))
  144.     rsBuf.Write(s.Bytes())
  145.  
  146.     buf := bytes.NewBuffer([]byte{})
  147.  
  148.     buf.WriteRune(48)
  149.     putLength(buf, len(rsBuf.Bytes()))
  150.     buf.Write(rsBuf.Bytes())
  151.  
  152.     fmt.Println("6666: ", base64.StdEncoding.EncodeToString(buf.Bytes()))
  153.  
  154.     return buf.Bytes()
  155. }
RAW Paste Data
Top