mirror of
https://github.com/gocsaf/csaf.git
synced 2025-12-22 11:55:40 +01:00
Moved writing files code to file source file.
This commit is contained in:
parent
05475f5509
commit
b8d51da4cb
2 changed files with 41 additions and 35 deletions
|
|
@ -2,16 +2,12 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/sha256"
|
|
||||||
"crypto/sha512"
|
|
||||||
"embed"
|
"embed"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"hash"
|
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -116,20 +112,6 @@ func loadCSAF(r *http.Request) (string, []byte, error) {
|
||||||
return cleanFileName(handler.Filename), buf.Bytes(), nil
|
return cleanFileName(handler.Filename), buf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeHash(fname, name string, h hash.Hash, data []byte) error {
|
|
||||||
|
|
||||||
if _, err := io.Copy(h, bytes.NewReader(data)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
f, err := os.Create(fname)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
fmt.Fprintf(f, "%x %s\n", h.Sum(nil), name)
|
|
||||||
return f.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *controller) loadCryptoKey() (*crypto.Key, error) {
|
func (c *controller) loadCryptoKey() (*crypto.Key, error) {
|
||||||
f, err := os.Open(c.cfg.Key)
|
f, err := os.Open(c.cfg.Key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -262,23 +244,7 @@ func (c *controller) upload(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
fname := filepath.Join(subDir, newCSAF)
|
fname := filepath.Join(subDir, newCSAF)
|
||||||
|
|
||||||
// Write the file itself.
|
if err := writeHashedFile(fname, newCSAF, data, armored); err != nil {
|
||||||
if err := ioutil.WriteFile(fname, data, 0644); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write SHA256 sum.
|
|
||||||
if err := writeHash(fname+".sha256", newCSAF, sha256.New(), data); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write SHA512 sum.
|
|
||||||
if err := writeHash(fname+".sha512", newCSAF, sha512.New(), data); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write signature.
|
|
||||||
if err := ioutil.WriteFile(fname+".asc", []byte(armored), 0644); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,14 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/sha256"
|
||||||
|
"crypto/sha512"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"hash"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
@ -174,3 +180,37 @@ func mkUniq(prefix string, create func(string) error) (string, error) {
|
||||||
|
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func writeHash(fname, name string, h hash.Hash, data []byte) error {
|
||||||
|
|
||||||
|
if _, err := io.Copy(h, bytes.NewReader(data)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.Create(fname)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Fprintf(f, "%x %s\n", h.Sum(nil), name)
|
||||||
|
return f.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeHashedFile(fname, name string, data []byte, armored string) error {
|
||||||
|
// Write the file itself.
|
||||||
|
if err := ioutil.WriteFile(fname, data, 0644); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// Write SHA256 sum.
|
||||||
|
if err := writeHash(fname+".sha256", name, sha256.New(), data); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// Write SHA512 sum.
|
||||||
|
if err := writeHash(fname+".sha512", name, sha512.New(), data); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// Write signature.
|
||||||
|
if err := ioutil.WriteFile(fname+".asc", []byte(armored), 0644); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue