Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial implementation of Argon2 (ported from bc-java) #520

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions crypto/src/crypto/ICharToByteConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace Org.BouncyCastle.Crypto
{
public interface ICharToByteConverter
{
/**
* Return the type of the conversion.
*
* @return a type name for the conversion.
*/
string GetName();

/**
* Return a byte encoded representation of the passed in password.
*
* @param password the characters to encode.
* @return a byte encoding of password.
*/
byte[] Convert(char[] password);
}

public static class CharToByteConverterExtensions
{

/**
* Return a byte encoded representation of the passed in password.
*
* @param password the string to encode.
* @return a byte encoding of password.
*/
public static byte[] Convert(this ICharToByteConverter converter, string password)
{
return converter.Convert(password.ToCharArray());
}
}
}
44 changes: 44 additions & 0 deletions crypto/src/crypto/PasswordConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Text;

namespace Org.BouncyCastle.Crypto
{
public class PasswordConverter
: ICharToByteConverter
{
private readonly string name;
private readonly Func<char[], byte[]> converterFunction;

public PasswordConverter(string name, Func<char[], byte[]> converterFunction)
{
this.name = name;
this.converterFunction = converterFunction;
}

public byte[] Convert(char[] password)
{
return converterFunction.Invoke(password);
}

public string GetName()
{
return name;
}

public readonly static ICharToByteConverter ASCII = new PasswordConverter("ASCII", PbeParametersGenerator.Pkcs5PasswordToBytes);

public readonly static ICharToByteConverter UTF8 = new PasswordConverter("UTF8", PbeParametersGenerator.Pkcs5PasswordToUtf8Bytes);

public readonly static ICharToByteConverter PKCS12 = new PasswordConverter("PKCS12", PbeParametersGenerator.Pkcs12PasswordToBytes);

public readonly static ICharToByteConverter UTF32 = new PasswordConverter("UTF32", Encoding.UTF32.GetBytes);

public readonly static ICharToByteConverter Unicode = new PasswordConverter("Unicode", Encoding.Unicode.GetBytes);

public readonly static ICharToByteConverter BigEndianUnicode = new PasswordConverter("BigEndianUnicode", Encoding.BigEndianUnicode.GetBytes);

#if NET6_0_OR_GREATER
public readonly static ICharToByteConverter Latin1 = new PasswordConverter("Latin1", Encoding.Latin1.GetBytes);
#endif
}
}
Loading