code: func (e *Encoder) Encode(db *Database) error {
  	// Calculate transformed key to make HMAC and encrypt
  	transformedKey, err := db.getTransformedKey()
  	if err != nil {
  		return err
  	}
  
  	// Write header then hashes before decode content (necessary to update HeaderHash)
  	// db.Header writeTo will change its hash
  	if err = db.Header.writeTo(e.w); err != nil {
  		return err
  	}
  
  	// Update header hash into db.Hashes then write the data
  	hash := db.Header.GetSha256()
  	if db.Header.IsKdbx4() {
  		db.Hashes.Sha256 = hash
  
  		hmacKey := buildHmacKey(db, transformedKey)
  		hmacHash := db.Header.GetHmacSha256(hmacKey)
  		db.Hashes.Hmac = hmacHash
  
  		if err = db.Hashes.writeTo(e.w); err != nil {
  			return err
  		}
  	} else {
  		db.Content.Meta.HeaderHash = base64.StdEncoding.EncodeToString(hash[:])
  	}
  
  	// Encode xml and append header to the top
  	rawContent, err := xml.MarshalIndent(db.Content, "", "\t")
  	if err != nil {
  		return err
  	}
  	rawContent = append(xmlHeader, rawContent...)
  
  	// Write InnerHeader (Kdbx v4)
  	if db.Header.IsKdbx4() {
  		var ih bytes.Buffer
  		if err = db.Content.InnerHeader.writeTo(&ih); err != nil {
  			return err
  		}
  
  		rawContent = append(ih.Bytes(), rawContent...)
  	}
  
  	// Encode raw content
  	encodedContent, err := encodeRawContent(db, rawContent, transformedKey)
  	if err != nil {
  		return err
  	}
  
  	// Writes the encrypted database content
  	_, err = e.w.Write(encodedContent)
  	return err
  }
docs: // Encode writes db to e's internal writer