| 48 | } |
| 49 | |
| 50 | func OpenBundle(data []byte, key []byte) ([]byte, error) { |
| 51 | if len(data) < chacha20poly1305.NonceSizeX { |
| 52 | return nil, errors.New("invalid bundle, too small") |
| 53 | } |
| 54 | ahead, err := chacha20poly1305.NewX(key[:]) |
| 55 | if err != nil { |
| 56 | return nil, err |
| 57 | } |
| 58 | nonce := data[:chacha20poly1305.NonceSizeX] |
| 59 | cipherText := data[chacha20poly1305.NonceSizeX:] |
| 60 | bundle, err := ahead.Open(make([]byte, 0), nonce, cipherText, make([]byte, 0)) |
| 61 | if err != nil { |
| 62 | return nil, err |
| 63 | } |
| 64 | return bundle, nil |
| 65 | } |
| 66 | |
| 67 | // BundleConfig first signs the config with the root private key, ensuring the authenticity, then encrypts the message using the bytes of the root public key as the shared key, offering some level of privacy. (assuming the root public key is not shared widely) |
| 68 | func BundleConfig(config string, rootKey NyPrivateKey) (string, error) { |