aboutsummaryrefslogtreecommitdiff
path: root/internal/pathiv/pathiv.go
blob: d2d90a234cf1e277f26272843bd10b6e32238be0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package pathiv

import (
	"crypto/sha256"

	"github.com/rfjakob/gocryptfs/internal/nametransform"
)

type Purpose string

const (
	PurposeDirIV     Purpose = "DIRIV"
	PurposeFileID    Purpose = "FILEID"
	PurposeSymlinkIV Purpose = "SYMLINKIV"
	PurposeBlock0IV  Purpose = "BLOCK0IV"
)

// Derive derives an IV from an encrypted path by hashing it with sha256
func Derive(path string, purpose Purpose) []byte {
	// Use null byte as separator as it cannot occur in the path
	extended := []byte(path + "\000" + string(purpose))
	hash := sha256.Sum256(extended)
	return hash[:nametransform.DirIVLen]
}