Coder Social home page Coder Social logo

Comments (2)

Xor-el avatar Xor-el commented on August 25, 2024

Hello @Noobaxx apologies for the late response, just seeing this issue now.

there were a couple of issues with your original code.

you can find the C# code below and the updated Delphi one that produces same result as the C# one.

// See https://aka.ms/new-console-template for more information
using Microsoft.AspNetCore.Cryptography.KeyDerivation;

static string HashUsingPbkdf2(string password, byte[] salt)
{
    byte[] derivedKey = KeyDerivation.Pbkdf2(password, salt, KeyDerivationPrf.HMACSHA256, iterationCount: 100000, 32);

    return Convert.ToBase64String(derivedKey);
}

var salt = new byte[] { 1, 2, 3, 4, 5 };
var saltAsBase64String = Convert.ToBase64String(salt);
var saltStringAsBytes = Convert.FromBase64String(saltAsBase64String);

Console.WriteLine(HashUsingPbkdf2("1, 2, 3, 4, 5", saltStringAsBytes));

Console.ReadLine();
program Project1;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  HlpHashFactory,
  HlpIHash,
  HlpIHashInfo,
  System.SysUtils,
  System.NetEncoding;

function HashUsingPbkdf2(Password: string; Salt: TBytes): string;

var
  PBKDF2: IPBKDF2_HMAC;
  PassBytes, derivedKey: TBytes;
  HashInstance: IHash;

begin
  PassBytes := TEncoding.UTF8.GetBytes(Password);

  HashInstance := THashFactory.TCrypto.CreateSHA2_256();

  PBKDF2 := TKDF.TPBKDF2_HMAC.CreatePBKDF2_HMAC(HashInstance, PassBytes,
    Salt, 100000);

  derivedKey := PBKDF2.GetBytes(32);

  Result := TBase64Encoding.Base64.EncodeBytesToString(derivedKey);
end;

begin
var
 salt, saltStringAsBytes: TBytes;
 var
 saltAsBase64String: string;
  try
    { TODO -oUser -cConsole Main : Insert code here }

    salt := TBytes.Create(1, 2, 3, 4, 5);

    saltAsBase64String := TBase64Encoding.Base64.EncodeBytesToString(salt);

    saltStringAsBytes := TBase64Encoding.Base64.DecodeStringToBytes(saltAsBase64String);

    Writeln(saltAsBase64String);

    Writeln(HashUsingPbkdf2('1, 2, 3, 4, 5', saltStringAsBytes));
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;

end.

from hashlib4pascal.

Noobaxx avatar Noobaxx commented on August 25, 2024

Thank you very much.

I now understand my mistakes

from hashlib4pascal.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.