github.com/pion/dtls/v2@v2.2.12/pkg/protocol/handshake/cipher_suite.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
     2  // SPDX-License-Identifier: MIT
     3  
     4  package handshake
     5  
     6  import "encoding/binary"
     7  
     8  func decodeCipherSuiteIDs(buf []byte) ([]uint16, error) {
     9  	if len(buf) < 2 {
    10  		return nil, errBufferTooSmall
    11  	}
    12  	cipherSuitesCount := int(binary.BigEndian.Uint16(buf[0:])) / 2
    13  	rtrn := make([]uint16, cipherSuitesCount)
    14  	for i := 0; i < cipherSuitesCount; i++ {
    15  		if len(buf) < (i*2 + 4) {
    16  			return nil, errBufferTooSmall
    17  		}
    18  
    19  		rtrn[i] = binary.BigEndian.Uint16(buf[(i*2)+2:])
    20  	}
    21  	return rtrn, nil
    22  }
    23  
    24  func encodeCipherSuiteIDs(cipherSuiteIDs []uint16) []byte {
    25  	out := []byte{0x00, 0x00}
    26  	binary.BigEndian.PutUint16(out[len(out)-2:], uint16(len(cipherSuiteIDs)*2))
    27  	for _, id := range cipherSuiteIDs {
    28  		out = append(out, []byte{0x00, 0x00}...)
    29  		binary.BigEndian.PutUint16(out[len(out)-2:], id)
    30  	}
    31  	return out
    32  }