AES_GCM 使用 webcrypto 加密 API

AES_GCM encryption using webcrypto API

我遇到了一个 AES_GCM 实现,其加密函数如下:

async function encryptMessage(key){
    let encodedMessage = encodeMessage();
    cipherText = await window.crypto.subtle.encrypt(
        {
            name: "AES-GCM",
            iv: window.crypto.getRandomValues(new Uint8Array(12))
        },
        key,
        encodedMessage
    );
    let buffer = new Uint8Array(cipherText, 0, 5);
    console.log(buffer);
    console.log(btoa(buffer));
}

我的问题是,为什么

new Uint8Array(cipherText, 0, 5);

有参数0和5,上网搜了一下,发现这几个参数代表一个视图。但为什么只有 0 和 5?我们可以使用这些以外的数字吗?

我认为该示例来自 https://github.com/mdn/dom-examples/blob/master/web-crypto/encrypt-decrypt/aes-gcm.js,它是 MDN 示例的一部分。

我的理解是他们只是向用户显示前几个字符,以证明它已被加密。您会看到真正的内容保存在 cipherText 中,它与 iv 一起是一个模块级变量。