
#include <gcrypt.h>
#include <stdio.h>
#include "swisscrypt.h"


/*
 @param in Pointer to SWISS_PWDLEN Bytes of message
 @param out Pointer to SWISS_PWDLEN Bytes of memory for storing digest
 @return same as out, or NULL if an error occurred
*/
char *swiss_pwd_hash(char *const out, const char *in)
{
      char *digest;
      gcry_md_hd_t mcontext;
      
      gcry_md_open(&mcontext, GCRY_MD_SHA1, 0);

      
      gcry_md_write(mcontext, (void*)in, SWISS_PWDLEN);
      digest = gcry_md_read(mcontext, 0);
      
      memcpy(out, digest, SWISS_PWDLEN);
      
      gcry_md_close(mcontext);
      
      return out;
}

char *swiss_pwd_hash_iterate(char *out, int i, const char *in){
      char a[SWISS_PWDLEN];

      memcpy((char*)a, in, SWISS_PWDLEN);
      for(;i > 0;i--){
	    memcpy(a, swiss_pwd_hash(out,a), SWISS_PWDLEN);
      }
      return out;
}

