package foreign.util;

import nova.Machine;
import nova.Store;
import nova.special;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Warranty & Liability
 * To the extent permitted by applicable law and unless explicitly
 * otherwise agreed upon, XLOG Technologies AG makes no warranties
 * regarding the provided information. XLOG Technologies AG assumes
 * no liability that any problems might be solved with the information
 * provided by XLOG Technologies AG.
 *
 * Rights & License
 * All industrial property rights regarding the information - copyright
 * and patent rights in particular - are the sole property of XLOG
 * Technologies AG. If the company was not the originator of some
 * excerpts, XLOG Technologies AG has at least obtained the right to
 * reproduce, change and translate the information.
 *
 * Reproduction is restricted to the whole unaltered document. Reproduction
 * of the information is only allowed for non-commercial uses. Selling,
 * giving away or letting of the execution of the library is prohibited.
 * The library can be distributed as part of your applications and libraries
 * for execution provided this comment remains unchanged.
 *
 * Restrictions
 * Only to be distributed with programs that add significant and primary
 * functionality to the library. Not to be distributed with additional
 * software intended to replace any components of the library.
 *
 * Trademarks
 * Jekejeke is a registered trademark of XLOG Technologies AG.
 */
public final class seclib {

    /**
     * digest_open(D): internal only
     * The built-in succeeds in D with a new digest.
     */
    private static boolean test_digest_open(Object[] args) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            return Machine.exec_unify(args[0], digest);
        } catch (NoSuchAlgorithmException e) {
            throw Machine.make_error(new Store.Compound("existence_error",
                    new Object[]{"algorithm", "SHA-256"}));
        }
    }

    /**
     * digest_update(D, B): internal only
     * The built-in succeeds. As a side effect the digest D is updated by B.
     */
    private static boolean test_digest_update(Object[] args) {
        try {
            Object alpha = Machine.exec_build(args[0]);
            check_digest(alpha);
            Object beta = Machine.exec_build(args[1]);
            special.check_atom(beta);
            byte[] buffer = ((String)beta).getBytes("latin1");
            ((MessageDigest)alpha).update(buffer);
            return true;
        } catch (UnsupportedEncodingException e) {
            throw Machine.make_error(new Store.Compound("existence_error",
                    new Object[]{"encoding", "latin1"}));
        }
    }

    /**
     * Assure that the object is a digest.
     *
     * @param beta The object.
     */
    private static void check_digest(Object beta) {
        if (!(beta instanceof MessageDigest)) {
            Machine.check_nonvar(beta);
            beta = Store.copy_term(beta);
            throw Machine.make_error(new Store.Compound("type_error",
                    new Object[]{"digest", beta}));
        }
    }

    /**
     * digest_close(D, B): internal only
     * The built-in succeeds in B with the result of the digest D.
     */
    private static boolean test_digest_close(Object[] args) {
        try {
            Object alpha = Machine.exec_build(args[0]);
            check_digest(alpha);
            byte[] buffer = ((MessageDigest)alpha).digest();
            return Machine.exec_unify(args[1], new String(buffer,"latin1"));
        } catch (UnsupportedEncodingException e) {
            throw Machine.make_error(new Store.Compound("existence_error",
                    new Object[]{"encoding", "latin1"}));
        }
    }

    /******************************************************************/
    /* Sec Lib Init                                                   */
    /******************************************************************/

    public static void main() {
        Store.add("digest_open", 1, special.make_check(seclib::test_digest_open));
        Store.add("digest_update", 2, special.make_check(seclib::test_digest_update));
        Store.add("digest_close", 2, special.make_check(seclib::test_digest_close));
    }

}
