Search code examples
javascriptencryptionlibsodium

How to use/install sodium-plus


I have an issue, I don't understand how to "install" sodium-plus for using on my website.

I installed :

<script src="https://cdn.jsdelivr.net/npm/sodium-plus@0.9.0/index.min.js"></script>

<script type="text/javascript" src="/require.js"></script>

and I want to test this script :

<script>
(async function() {
    if (!window.sodium) window.sodium = await SodiumPlus.auto();

    // You can now use the sodium object here.

    // Just some example code to ensure it s running:
    let random = await sodium.randombytes_buf(32);
    let hash = await sodium.crypto_generichash(
    "hello world");
    console.log({
        "random": random.toString("hex"),
        "hash": hash.toString("hex")
    });
})();
</script>

But I have two errors :

Uncaught ReferenceError: module is not defined Uncaught (in promise)

TypeError: Cannot read properties of undefined (reading 'auto')


Solution

  • It works with this link :

    src="https://cdn.jsdelivr.net/npm/sodium-plus@0.9.0/dist/sodium-plus.min.js"

    and I delete the code :

    let { SodiumPlus } = "sodium-plus";
    require(["sodium-plus"], function(result){
        SodiumPlus = result;
    });
    

    But I keep the requirejs script from this website (at the bottom of my scripts because there is sometimes an issue with jQuery) :

    https://cdnjs.com/libraries/require.js

    And after, to avoid an issue about requirejs script, I do this :

    define(function (require) {
        const { SodiumPlus } = require("sodium-plus");
    });
    let sodium;
    

    Not this :

    const { SodiumPlus } = require('sodium-plus');
    let sodium;