' Gambas module file

' https://tools.ietf.org/html/draft-josefsson-scrypt-kdf-00#page-10
Private Const PBKDF2_SHA256_80000_64 As String = ""
    "\x4d\xdc\xd8\xf6\x0b\x98\xbe\x21\x83\x0c\xee\x5e\xf2\x27\x01\xf9"
    "\x64\x1a\x44\x18\xd0\x4c\x04\x14\xae\xff\x08\x87\x6b\x34\xab\x56"
    "\xa1\xd4\x25\xa1\x22\x58\x33\x54\x9a\xdb\x84\x1b\x51\xc9\xb3\x17"
    "\x6a\x27\x2b\xde\xbb\xa1\xd0\x78\x47\x8f\x62\xb3\x97\xf3\x3c\x8d"

Private Const SCRYPT_1024_8_64 As String = ""
    "\xfd\xba\xbe\x1c\x9d\x34\x72\x00\x78\x56\xe7\x19\x0d\x01\xe9\xfe"
    "\x7c\x6a\xd7\xcb\xc8\x23\x78\x30\xe7\x73\x76\x63\x4b\x37\x31\x62"
    "\x2e\xaf\x30\xd9\x2e\x22\xa3\x88\x6f\xf1\x09\x27\x9d\x98\x30\xda"
    "\xc7\x27\xaf\xb9\x4a\x83\xee\x6d\x83\x60\xcb\xdf\xa2\xcc\x06\x40"

Public Sub Main()

  Dim p, s As String
  Dim a As New String[]

  Test.Plan(4)

  p = OpenSSL.Pbkdf2("Password", "NaCl", 80000, 64, "SHA256")
  s = OpenSSL.Scrypt("password", "NaCl", 1024, 8, 16, 64)
  Assert.Equals(Base64$(p), Base64$(PBKDF2_SHA256_80000_64), "PBKDF2 matches test vector")
  Assert.Equals(Base64$(s), Base64$(SCRYPT_1024_8_64), "scrypt matches test vector")

  Test.Note("Testing again for memory management")
  For i As Integer = 1 To 2 ^ 10
    a.Add(OpenSSL.RandomBytes(2 ^ 14))
  Next

  Assert.Equals(Base64$(p), Base64$(PBKDF2_SHA256_80000_64), "PBKDF2 matches test vector")
  Assert.Equals(Base64$(s), Base64$(SCRYPT_1024_8_64), "scrypt matches test vector")

End
