簡體   English   中英

如何使用 Firebase Cloud Function 加密字符串

[英]How to encrypt a String with a Firebase Cloud Function

我正在嘗試加密我的 Firebase Cloud Function 中的一個字符串。我很樂意為此使用 SHA-256 或 AES-256。 但是我還沒有找到正確的方法。

exports.myfunction = functions.https.onCall((data, context) => {  
  const someString = "Hello World!"
  const encryptedString = // How could I do this here?

  return encryptedString
})

因此,任何幫助表示贊賞。 謝謝。

一個不錯的選擇可能是 crypto 模塊。 它提供加密功能,包括一組用於 OpenSSL 的 hash、HMAC、密碼、解密、簽名和驗證功能的包裝器。

您可以使用crypto.createHash(algorithm\[, options\])來加密字符串。 查看有關此 function 的文檔

這是最終的解決方案:

// Use `require('crypto')` to access this module.
const crypto = require('crypto');

exports.myfunction = functions.https.onCall((data, context) => {  
  const secret = 'Hello World!';
  const hash = crypto.createHash('sha256')
                   .update(pwd)
                   .digest('base64'); // you can also use 'hex'
  return hash
})

另請查看官方文檔

使用此命令安裝 crypto-js。 npm install crypto-js

然后導入它並對您的字符串進行編碼。

const AES = require("crypto-js/aes");
const SHA256 = require("crypto-js/sha256");

exports.myfunction = functions.https.onCall((data, context) => {  
  const someString = "Hello World!";
  const encryptedString = SHA256(someString);
  // or
  const encryptedString = AES(someString);

  return encryptedString;
})

您可以查看Google Tink 庫

一種多語言、跨平台的庫,提供安全、易於正確使用且難以(呃)濫用的加密 API。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM