001/*
002 * Syncany, www.syncany.org
003 * Copyright (C) 2011-2016 Philipp C. Heckel <philipp.heckel@gmail.com>
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package org.syncany.config.to;
019
020import java.io.File;
021
022import org.simpleframework.xml.Element;
023import org.simpleframework.xml.Root;
024import org.simpleframework.xml.core.Commit;
025import org.simpleframework.xml.core.Complete;
026import org.simpleframework.xml.core.Persist;
027import org.simpleframework.xml.core.Persister;
028import org.syncany.config.ConfigException;
029import org.syncany.util.StringUtil;
030
031/**
032 * The master transfer object is used to create and load the master file
033 * from/to XML. The master file only contains the salt for the master key.
034 *
035 * <p>The master file is stored locally and on the remote storage. The salt
036 * is used to create the master key from a password.
037 *
038 * <p>It uses the Simple framework for XML serialization, and its corresponding
039 * annotation-based configuration.
040 *
041 * @see <a href="http://simple.sourceforge.net/">Simple framework</a>
042 * @author Philipp C. Heckel (philipp.heckel@gmail.com)
043 */
044@Root(name = "master", strict = false)
045public class MasterTO {
046        @Element(name = "salt", required = false)
047        private String saltEncoded;
048        private byte[] salt;
049
050        public MasterTO() {
051                // Required default constructor
052        }
053
054        public MasterTO(byte[] salt) {
055                this.salt = salt;
056        }
057
058        public byte[] getSalt() {
059                return salt;
060        }
061
062        public void setSalt(byte[] salt) {
063                this.salt = salt;
064        }
065
066        public void save(File file) throws ConfigException {
067                try {
068                        new Persister().write(this, file);
069                }
070                catch (Exception e) {
071                        throw new ConfigException("Cannot write masterTO to file " + file, e);
072                }
073        }
074
075        @Persist
076        public void prepare() {
077                saltEncoded = (salt != null) ? StringUtil.toHex(salt) : null;
078        }
079
080        @Complete
081        public void release() {
082                saltEncoded = null;
083        }
084
085        @Commit
086        public void commit() {
087                salt = (saltEncoded != null) ? StringUtil.fromHex(saltEncoded) : null;
088        }
089}