Table of Contents
- OtpSecret
- Properties
- Constructors
- Methods
- ToString()
- ToString(IEncoder)
- Equals(OtpSecret)
- Equals(Object)
- Equals(Byte[])
- GetHashCode()
- Dispose()
- GetSchema()
- ReadXml(XmlReader)
- WriteXml(XmlWriter)
- CreateCopy(OtpSecret)
- CreateNew()
- CreateNew(Int32)
- Parse(String)
- Parse(String, IEncoder)
- TryParse(String, OtpSecret&)
- TryParse(String, IEncoder, OtpSecret&)
- FromBytes(Byte[])
OtpSecret
Namespace: SimpleOTP
Represents a one-time password secret.
public class OtpSecret : System.IEquatable`1[[SimpleOTP.OtpSecret, SimpleOTP, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null]], System.IEquatable`1[[System.Byte[], System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Xml.Serialization.IXmlSerializable, System.IDisposable
Inheritance Object → OtpSecret
Implements IEquatable<OtpSecret>, IEquatable<Byte[]>, IXmlSerializable, IDisposable
Attributes SerializableAttribute, NullableContextAttribute, NullableAttribute, JsonConverterAttribute
Properties
DefaultEncoder
Gets or sets the default encoder for parsing/encoding/serializing secrets.
public static IEncoder DefaultEncoder { get; set; }
Property Value
Constructors
OtpSecret()
Initializes a new instance of the OtpSecret class with a default length of 20 bytes (160 bits).
public OtpSecret()
OtpSecret(Int32)
Initializes a new instance of the OtpSecret class with a random secret of the specified length.
public OtpSecret(int length)
Parameters
length Int32
The length of the secret in bytes.
Exceptions
ArgumentOutOfRangeException
length is less than 1.
Remarks:
20 bytes (160 bits) is the recommended key length specified by RFC 4226. Minimal recommended length is 16 bytes (128 bits).
OtpSecret(Byte[])
Initializes a new instance of the OtpSecret class from a byte array.
public OtpSecret(Byte[] secret)
Parameters
secret Byte[]
The byte array.
Exceptions
ArgumentNullException
secret is null or empty.
OtpSecret(String)
Initializes a new instance of the OtpSecret class from a Base32-encoded string (RFC 4648 §6).
public OtpSecret(string secret)
Parameters
secret String
The Base32-encoded string.
Exceptions
ArgumentNullException
secret is null.
ArgumentException
secret is empty or contains invalid characters or only whitespace.
OtpSecret(String, IEncoder)
Initializes a new instance of the OtpSecret class from an encoded string.
public OtpSecret(string secret, IEncoder encoder)
Parameters
secret String
The encoded string.
encoder IEncoder
The encoder.
Exceptions
ArgumentNullException
secret is null or empty.
ArgumentException
secret is empty or contains invalid characters or only whitespace.
Methods
ToString()
Returns the Base32-encoded string representation of the current OtpSecret object.
public string ToString()
Returns
String
The Base32-encoded string representation of the current OtpSecret object.
ToString(IEncoder)
Returns the string representation of the current OtpSecret object.
public string ToString(IEncoder encoder)
Parameters
encoder IEncoder
The encoder.
Returns
String
The string representation of the current OtpSecret object.
Equals(OtpSecret)
public bool Equals(OtpSecret other)
Parameters
other OtpSecret
Returns
Equals(Object)
public bool Equals(object obj)
Parameters
obj Object
Returns
Equals(Byte[])
public bool Equals(Byte[] other)
Parameters
other Byte[]
Returns
GetHashCode()
public int GetHashCode()
Returns
Dispose()
public void Dispose()
GetSchema()
public XmlSchema GetSchema()
Returns
XmlSchema
ReadXml(XmlReader)
public void ReadXml(XmlReader reader)
Parameters
reader XmlReader
WriteXml(XmlWriter)
public void WriteXml(XmlWriter writer)
Parameters
writer XmlWriter
CreateCopy(OtpSecret)
Creates a copy of the specified OtpSecret object.
public static OtpSecret CreateCopy(OtpSecret source)
Parameters
source OtpSecret
The OtpSecret object to copy.
Returns
OtpSecret
A copy of the specified OtpSecret object.
CreateNew()
Creates a new random OtpSecret object with a default length of 20 bytes.
public static OtpSecret CreateNew()
Returns
OtpSecret
A new random OtpSecret object.
Remarks:
20 bytes (160 bits) is the recommended key length specified by RFC 4226. Minimal recommended length is 16 bytes (128 bits).
CreateNew(Int32)
Creates a new random OtpSecret object with the specified length.
public static OtpSecret CreateNew(int length)
Parameters
length Int32
The length of the secret in bytes.
Returns
OtpSecret
A new random OtpSecret object.
Exceptions
ArgumentOutOfRangeException
length is less than 1.
Remarks:
20 bytes (160 bits) is the recommended key length specified by RFC 4226. Minimal recommended length is 16 bytes (128 bits).
Parse(String)
Parses a Base32-encoded string into an OtpSecret object.
public static OtpSecret Parse(string secret)
Parameters
secret String
The Base32-encoded string.
Returns
OtpSecret
An OtpSecret object.
Exceptions
ArgumentNullException
secret is null.
ArgumentException
secret is empty or contains invalid characters or only whitespace.
Parse(String, IEncoder)
Parses a Base32-encoded string into an OtpSecret object.
public static OtpSecret Parse(string secret, IEncoder encoder)
Parameters
secret String
The Base32-encoded string.
encoder IEncoder
The encoder.
Returns
OtpSecret
An OtpSecret object.
Exceptions
ArgumentNullException
secret is null.
ArgumentException
secret is empty or contains invalid characters or only whitespace.
TryParse(String, OtpSecret&)
Tries to parse a Base32-encoded string into an OtpSecret object.
public static bool TryParse(string secret, OtpSecret& otpSecret)
Parameters
secret String
The Base32-encoded string.
otpSecret OtpSecret&
When this method returns, contains the OtpSecret object, if the conversion succeeded, or default if the conversion failed.
Returns
Boolean
true if secret was converted successfully; otherwise, false.
TryParse(String, IEncoder, OtpSecret&)
Tries to parse a Base32-encoded string into an OtpSecret object.
public static bool TryParse(string secret, IEncoder encoder, OtpSecret& otpSecret)
Parameters
secret String
The Base32-encoded string.
encoder IEncoder
The encoder.
otpSecret OtpSecret&
When this method returns, contains the OtpSecret object, if the conversion succeeded, or default if the conversion failed.
Returns
Boolean
true if secret was converted successfully; otherwise, false.
FromBytes(Byte[])
Creates a new OtpSecret object from a byte array.
public static OtpSecret FromBytes(Byte[] secret)
Parameters
secret Byte[]
The byte array.
Returns
OtpSecret
An OtpSecret object.
Exceptions
ArgumentNullException
secret is null.
ArgumentOutOfRangeException
secret is empty.
©2025 Eugene Fox. Licensed under MIT license