base58check.mx raw

   1  // Copyright (c) 2013-2014 The btcsuite developers
   2  // Use of this source code is governed by an ISC
   3  // license that can be found in the LICENSE file.
   4  
   5  package base58
   6  
   7  import (
   8  	"errors"
   9  
  10  	"crypto/sha256"
  11  )
  12  
  13  // ErrChecksum indicates that the checksum of a check-encoded string does not verify against
  14  // the checksum.
  15  var ErrChecksum = errors.New("checksum error")
  16  
  17  // ErrInvalidFormat indicates that the check-encoded string has an invalid format.
  18  var ErrInvalidFormat = errors.New("invalid format: version and/or checksum bytes missing")
  19  
  20  // checksum: first four bytes of sha256^2
  21  func checksum(input []byte) (cksum [4]byte) {
  22  	h := sha256.Sum256(input)
  23  	h2 := sha256.Sum256(h[:])
  24  	copy(cksum[:], h2[:4])
  25  	return
  26  }
  27  
  28  // CheckEncode prepends a version byte and appends a four byte checksum.
  29  func CheckEncode(input []byte, version byte) string {
  30  	b := []byte{:0:1+len(input)+4}
  31  	b = append(b, version)
  32  	b = append(b, input...)
  33  	cksum := checksum(b)
  34  	b = append(b, cksum[:]...)
  35  	return Encode(b)
  36  }
  37  
  38  // CheckDecode decodes a string that was encoded with CheckEncode and verifies the checksum.
  39  func CheckDecode(input string) (result []byte, version byte, err error) {
  40  	decoded := Decode(input)
  41  	if len(decoded) < 5 {
  42  		return nil, 0, ErrInvalidFormat
  43  	}
  44  	version = decoded[0]
  45  	var cksum [4]byte
  46  	copy(cksum[:], decoded[len(decoded)-4:])
  47  	if checksum(decoded[:len(decoded)-4]) != cksum {
  48  		return nil, 0, ErrChecksum
  49  	}
  50  	payload := decoded[1 : len(decoded)-4]
  51  	result = append(result, payload...)
  52  	return
  53  }
  54