using System;
using System.Text;
namespace S22.Sasl.Mechanisms.Srp {
///
/// Represents an UTF-8 string as is described in the SRP specification
/// (3.5 Text, p.6).
///
internal class Utf8String {
///
/// The value of the UTF-8 string.
///
public string Value;
///
/// Creates a new instance of the Utf8String class using the specified
/// string value.
///
/// The string to initialize the Utf8String instance
/// with.
public Utf8String(string s) {
Value = s;
}
///
/// Serializes this instance of the Utf8String class into a sequence of
/// bytes according to the requirements of the SRP specification.
///
/// A sequence of bytes representing this instance of the
/// Utf8String class.
/// Thrown if the string value exceeds
/// the maximum number of bytes allowed as per SRP specification.
/// SRP specification imposes a limit of 65535 bytes on the
/// string data after it has been encoded into a sequence of bytes
/// using an encoding of UTF-8.
public byte[] Serialize() {
byte[] b = Encoding.UTF8.GetBytes(Value);
ushort length = Convert.ToUInt16(b.Length);
return new ByteBuilder()
.Append(length, true)
.Append(b)
.ToArray();
}
}
}