github.com/cyverse/go-irodsclient@v0.13.2/irods/message/ssl_settings.go (about) 1 package message 2 3 import ( 4 "encoding/xml" 5 6 "golang.org/x/xerrors" 7 ) 8 9 // IRODSMessageSSLSettings stores ssl settings 10 type IRODSMessageSSLSettings struct { 11 EncryptionAlgorithm string 12 EncryptionKeySize uint32 13 SaltSize uint32 14 HashRounds uint32 15 } 16 17 // NewIRODSMessageSSLSettings creates a IRODSMessageSSLSettings message 18 func NewIRODSMessageSSLSettings(algorithm string, keySize int, saltSize int, hashRounds int) *IRODSMessageSSLSettings { 19 return &IRODSMessageSSLSettings{ 20 EncryptionAlgorithm: algorithm, 21 EncryptionKeySize: uint32(keySize), 22 SaltSize: uint32(saltSize), 23 HashRounds: uint32(hashRounds), 24 } 25 } 26 27 // GetBytes returns byte array 28 func (msg *IRODSMessageSSLSettings) GetBytes() ([]byte, error) { 29 xmlBytes, err := xml.Marshal(msg) 30 if err != nil { 31 return nil, xerrors.Errorf("failed to marshal irods message to xml: %w", err) 32 } 33 return xmlBytes, nil 34 } 35 36 // FromBytes returns struct from bytes 37 func (msg *IRODSMessageSSLSettings) FromBytes(bytes []byte) error { 38 err := xml.Unmarshal(bytes, msg) 39 if err != nil { 40 return xerrors.Errorf("failed to unmarshal xml to irods message: %w", err) 41 } 42 return nil 43 } 44 45 // GetMessage builds a message 46 func (msg *IRODSMessageSSLSettings) GetMessage() (*IRODSMessage, error) { 47 msgHeader := IRODSMessageHeader{ 48 Type: MessageType(msg.EncryptionAlgorithm), 49 MessageLen: msg.EncryptionKeySize, 50 ErrorLen: msg.SaltSize, 51 BsLen: msg.HashRounds, 52 IntInfo: 0, 53 } 54 55 return &IRODSMessage{ 56 Header: &msgHeader, 57 Body: nil, 58 }, nil 59 } 60 61 // FromMessage returns struct from IRODSMessage 62 func (msg *IRODSMessageSSLSettings) FromMessage(msgIn *IRODSMessage) error { 63 if msgIn.Body == nil { 64 return xerrors.Errorf("empty message body") 65 } 66 67 msg.EncryptionAlgorithm = string(msgIn.Header.Type) 68 msg.EncryptionKeySize = msgIn.Header.MessageLen 69 msg.SaltSize = msgIn.Header.ErrorLen 70 msg.HashRounds = msgIn.Header.BsLen 71 72 return nil 73 }