-
-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1c97d8
commit 83366a3
Showing
17 changed files
with
475 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package cipher | ||
|
||
import ( | ||
"crypto/rand" | ||
"errors" | ||
"io" | ||
) | ||
|
||
var ErrDecrypting = errors.New("unable to decrypt") | ||
|
||
type Cipher interface { | ||
Encrypt(plaintext []byte) ([]byte, error) | ||
Decrypt(ciphertext []byte) ([]byte, error) | ||
} | ||
|
||
func Random(b []byte) error { | ||
if _, err := io.ReadFull(rand.Reader, b); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func Rotator(latest Cipher, priors ...Cipher) Cipher { | ||
return &rotator{append([]Cipher{latest}, priors...)} | ||
} | ||
|
||
type rotator struct { | ||
ciphers []Cipher | ||
} | ||
|
||
// Decrypt with any past or present cipher | ||
func (r *rotator) Decrypt(ciphertext []byte) ([]byte, error) { | ||
for _, c := range r.ciphers { | ||
plaintext, err := c.Decrypt(ciphertext) | ||
if err != nil { | ||
if !errors.Is(err, ErrDecrypting) { | ||
return nil, err | ||
} | ||
continue | ||
} | ||
return plaintext, nil | ||
} | ||
return nil, ErrDecrypting | ||
} | ||
|
||
// Encrypt with the latest cipher | ||
func (r *rotator) Encrypt(plaintext []byte) ([]byte, error) { | ||
return r.ciphers[0].Encrypt(plaintext) | ||
} |
Oops, something went wrong.