| 
									
										
										
										
											2021-08-31 01:39:55 +08:00
										 |  |  | package encryption | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-02 21:08:09 +08:00
										 |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							|  |  |  | 	"crypto/sha256" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"golang.org/x/crypto/pbkdf2" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							|  |  |  | 	SaltLength = 8 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	AesCfb = "aes-cfb" | 
					
						
							|  |  |  | 	AesGcm = "aes-gcm" | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2021-10-07 22:33:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 19:16:39 +08:00
										 |  |  | // Internal must not be used for general purpose encryption.
 | 
					
						
							|  |  |  | // This service is used as an internal component for envelope encryption
 | 
					
						
							|  |  |  | // and for very specific few use cases that still require legacy encryption.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Unless there is any specific reason, you must use secrets.Service instead.
 | 
					
						
							|  |  |  | type Internal interface { | 
					
						
							| 
									
										
										
										
											2022-08-02 21:08:09 +08:00
										 |  |  | 	Cipher | 
					
						
							|  |  |  | 	Decipher | 
					
						
							| 
									
										
										
										
											2021-10-07 22:33:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	EncryptJsonData(ctx context.Context, kv map[string]string, secret string) (map[string][]byte, error) | 
					
						
							|  |  |  | 	DecryptJsonData(ctx context.Context, sjd map[string][]byte, secret string) (map[string]string, error) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	GetDecryptedValue(ctx context.Context, sjd map[string][]byte, key string, fallback string, secret string) string | 
					
						
							| 
									
										
										
										
											2021-08-31 01:39:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2022-08-02 21:08:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | type Cipher interface { | 
					
						
							|  |  |  | 	Encrypt(ctx context.Context, payload []byte, secret string) ([]byte, error) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type Decipher interface { | 
					
						
							|  |  |  | 	Decrypt(ctx context.Context, payload []byte, secret string) ([]byte, error) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type Provider interface { | 
					
						
							|  |  |  | 	ProvideCiphers() map[string]Cipher | 
					
						
							|  |  |  | 	ProvideDeciphers() map[string]Decipher | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // KeyToBytes key length needs to be 32 bytes
 | 
					
						
							|  |  |  | func KeyToBytes(secret, salt string) ([]byte, error) { | 
					
						
							|  |  |  | 	return pbkdf2.Key([]byte(secret), []byte(salt), 10000, 32, sha256.New), nil | 
					
						
							|  |  |  | } |