コード例 #1
0
ファイル: key.go プロジェクト: LegoShrimp/crypto
// Generate a new public/private keypair with the given ciphersuite
// and Save it to the application's previously-loaded configuration.
func (f *File) GenKey(keys *Keys, suite abstract.Suite) (KeyPair, error) {

	// Create the map if it doesn't exist
	//	if *keys == nil {
	//		*keys = make(map[string] KeyInfo)
	//	}

	// Create a fresh public/private keypair
	p := KeyPair{}
	p.Gen(suite, random.Stream)
	pubId := p.PubId()

	// Write the private key file
	secname := f.dirName + "/sec-" + pubId
	r := util.Replacer{}
	if err := r.Open(secname); err != nil {
		return KeyPair{}, err
	}
	defer r.Abort()

	// Write the secret key
	if err := suite.Write(r.File, &p.Secret); err != nil {
		return KeyPair{}, err
	}

	// Commit the secret key
	if err := r.Commit(); err != nil {
		return KeyPair{}, err
	}

	// Re-write the config file with the new public key
	*keys = append(*keys, KeyInfo{suite.String(), pubId})
	if err := f.Save(); err != nil {
		return KeyPair{}, err
	}

	return p, nil
}
コード例 #2
0
ファイル: file.go プロジェクト: LegoShrimp/crypto
// Re-save the (modified) configData loaded earlier with Load().
// Takes precautions to replace the old config file atomically
// to avoid config file corruption due to write errors or races.
func (f *File) Save() error {

	// Write the new config file
	filename := f.dirName + "/config"
	r := util.Replacer{}
	if err := r.Open(filename); err != nil {
		return err
	}
	defer r.Abort()

	// Encode the config
	enc := toml.NewEncoder(r.File)
	if err := enc.Encode(f.data); err != nil {
		return err
	}

	// Commit the new config
	if err := r.Commit(); err != nil {
		return err
	}

	return nil
}