Golang: convert byte array to big.Int

I am trying to create an RSA public key from a module and exponent stored in an array of bytes. After some experimentation, I have the following:

func bytes_to_int(b []byte) (acc uint64) {
  length := len(b)
  if length % 4 != 0 {
    extra := (4 - length % 4)
    b = append([]byte(strings.Repeat("\000", extra)), b...)
    length += extra
  }
  var block uint32
  for i := 0; i < length; i += 4 {
    block = binary.BigEndian.Uint32(b[i:i+4])
    acc = (acc << 32) + uint64(block)
  }
return
}

func main() {
  fmt.Println(bytes_to_int(data[:128]))
  fmt.Println(bytes_to_int(data[128:]))
}

This seems to work (although I'm not sure there is no better way). The next step was to convert it to using math / large to handle large numbers. I see the Lsh function to execute <<but cannot figure out how to add Uint32 (block) recursively to large.Int.

For reference, the public key I'm trying to import is a Mixmaster Key stored in keyring (pubring.mix): http://www.mixmin.net/draft-sassaman-mixmaster-XX.html#key-format http: //pinger.mixmin.net/pubring.mix

+4
3

Int.SetBytes big.int []byte.

func (z *Int) SetBytes(buf []byte) *Int

SetBytes buf , z z.

, big-endian , .

+3
import "math/big"

z := new(big.Int)
z.SetBytes(byteSliceHere)
+2

, SetBytes, , base64, .

Example:

func Base64ToInt(s string) (*big.Int, error) {
    data, err := base64.StdEncoding.DecodeString(s)
    if err != nil {
        return nil, err
    }
    i := new(big.Int)
    i.SetBytes(data)
    return i, nil
}
+1

Source: https://habr.com/ru/post/1548484/


All Articles