github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/encrypt/obscure/id.go (about)

     1  package obscure
     2  
     3  func (p *Obscure) EncodeID(id int64) []byte {
     4  	buf := make([]byte, 8)
     5  	binEnc.PutUint64(buf, uint64(id))
     6  	return p.EncodeToBytes(buf)
     7  }
     8  
     9  func (p *Obscure) DecodeID(src []byte) (int64, error) {
    10  	if len(src) == 0 {
    11  		return 0, ErrInvalidInput
    12  	}
    13  	buf, err := p.DecodeBytes(src)
    14  	if err != nil {
    15  		return 0, err
    16  	}
    17  	if len(buf) != 8 {
    18  		return 0, ErrInvalidInput
    19  	}
    20  	id := int64(binEnc.Uint64(buf))
    21  	return id, nil
    22  }