/*
 *
  ***** BEGIN LICENSE BLOCK *****
 
  Copyright (C) 2024 Olof Hagsand

  This file is part of CLIXON.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  Alternatively, the contents of this file may be used under the terms of
  the GNU General Public License Version 3 or later (the "GPL"),
  in which case the provisions of the GPL are applicable instead
  of those above. If you wish to allow use of your version of this file only
  under the terms of the GPL, and not to allow others to
  use your version of this file under the terms of Apache License version 2, 
  indicate your decision by deleting the provisions above and replace them with
  the  notice and other provisions required by the GPL. If you do not delete
  the provisions above, a recipient may use your version of this file under
  the terms of any one of the Apache License version 2 or the GPL.

  ***** END LICENSE BLOCK *****
 */

#ifdef HAVE_CONFIG_H
#include "clixon_config.h" /* generated by config & autoconf */
#endif

#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <openssl/sha.h>

/* cligen */
#include <cligen/cligen.h>

/* clixon */
#include "clixon_queue.h"
#include "clixon_hash.h"
#include "clixon_handle.h"
#include "clixon_yang.h"
#include "clixon_xml.h"
#include "clixon_err.h"
#include "clixon_digest.h"

/*! Given null-terminated string, return malloced NULL_terinated SHA digest string in hex
 *
 * @param[in]  str     Input null-terminated string
 * @param[out] hexstrp Output null-terminated digest HEX string
 * @retval     0       OK
 * @retval    -1       Error
 */
int
clixon_digest_hex(const char *str,
                  char      **hexstrp)
{
    int            retval = -1;
    unsigned char *md = NULL;
    cbuf          *cb = NULL;
    int            i;
    uint32_t       len;

#ifdef USE_SHA256
    len = SHA256_DIGEST_LENGTH;
#else
    len = SHA_DIGEST_LENGTH;
#endif
    if (str == NULL || hexstrp == NULL){
        clixon_err(OE_UNIX, EINVAL, "str or hexstr is NULL");
        return -1;
    }
    if ((md = calloc(len, sizeof(unsigned char))) == NULL){
        clixon_err(OE_UNIX, errno, "calloc");
        goto done;
    }
#ifdef USE_SHA256
    if (SHA256((unsigned char *)str, strlen(str), md) == NULL)
#else
    if (SHA1((unsigned char *)str, strlen(str), md) == NULL)
#endif
        {
        clixon_err(OE_UNIX, 0, "SHA256 error");
        goto done;
    }
    if ((cb = cbuf_new()) == NULL){
        clixon_err(OE_XML, errno, "cbuf_new");
        goto done;
    }
    for (i = 0; i < len; i++)
        cprintf(cb, "%02x", md[i]&0xff);
    if ((*hexstrp = strdup(cbuf_get(cb))) == NULL){
        clixon_err(OE_UNIX, errno, "strdup");
        goto done;
    }
    retval = 0;
 done:
    if (cb)
        cbuf_free(cb);
    if (md)
        free(md);
    return retval;
}
