Skip to content

Instantly share code, notes, and snippets.

@revolunet
Last active August 7, 2020 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save revolunet/655729c36620a3fa5f3780c4b60696d5 to your computer and use it in GitHub Desktop.
Save revolunet/655729c36620a3fa5f3780c4b60696d5 to your computer and use it in GitHub Desktop.
sealed-secret-encrypt not working
//
// attempt to convert original HybridEncrypt source // https://github.com/bitnami-labs/sealed-secrets/blob/946a69eb52f9874fe871d3ce08eb205726380931/pkg/crypto/crypto.go#L35
// online GO REPL : https://repl.it/@revolunet/sealed-secrets-HybridEncrypt#main.go
// algo description : https://github.com/bitnami-labs/sealed-secrets/blob/master/docs/crypto.md
//
// this give : type: 'Warning' reason: 'ErrUnsealFailed' Failed to unseal: no key could decrypt secret (VALUE)
//
const crypto = require("crypto");
const encrypt = async ({ publicKey, plainText, label }) => {
const key = crypto.createPublicKey(publicKey);
/*
sessionKey := make([]byte, sessionKeyBytes)
block, err := aes.NewCipher(sessionKey)
aed, err := cipher.NewGCM(block)
*/
const sessionKey = await crypto.randomBytes(32);
const iv = Buffer.alloc(32, 0);
const aed = crypto.createCipheriv("aes-256-gcm", sessionKey, iv);
/*
rsaCiphertext, err := rsa.EncryptOAEP(sha256.New(), rnd, pubKey, sessionKey, label)
*/
const rsaCiphertext = crypto.publicEncrypt(
{
key,
oaepHash: "SHA256",
oaepLabel: Buffer.from(label),
},
sessionKey
);
/*
ciphertext := make([]byte, 2)
binary.BigEndian.PutUint16(ciphertext, uint16(len(rsaCiphertext)))
ciphertext = append(ciphertext, rsaCiphertext...)
*/
const ciphertext = new Uint16Array(2);
ciphertext[0] = rsaCiphertext.length;
// zeroNonce := make([]byte, aed.NonceSize())
// ciphertext = aed.Seal(ciphertext, zeroNonce, plaintext, nil)
const toEncrypt = Buffer.from([...ciphertext, ...rsaCiphertext]);
const encrypted = aed.update(toEncrypt);
aed.final();
return encrypted.toString("base64");
};
encrypt({ publicKey, plainText: "hello, world", label: "" }).then(console.log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment