1
0
mirror of https://github.com/schollz/cowyo.git synced 2023-08-10 21:13:00 +03:00

4 Commits

Author SHA1 Message Date
Zack Scholl
d3101da4ac Add /exists to check if page exists 2017-07-05 10:49:06 -06:00
Zack Scholl
d69e37d1bc Update README 2017-06-29 15:34:56 -06:00
Zack Scholl
57e28b85cd Update README 2017-06-29 15:25:13 -06:00
Zack Scholl
9dbb83e8af Moved the encryption to its own package 2017-06-29 15:24:34 -06:00
7 changed files with 93 additions and 50 deletions

View File

@@ -5,7 +5,7 @@
width="260" height="80" border="0" alt="linkcrawler">
<br>
<a href="https://travis-ci.org/schollz/cowyo"><img src="https://img.shields.io/travis/schollz/cowyo.svg?style=flat-square" alt="Build Status"></a>
<a href="https://github.com/schollz/cowyo/releases/latest"><img src="https://img.shields.io/badge/version-2.2.0-brightgreen.svg?style=flat-square" alt="Version"></a>
<a href="https://github.com/schollz/cowyo/releases/latest"><img src="https://img.shields.io/badge/version-2.3.0-brightgreen.svg?style=flat-square" alt="Version"></a>
</p>
<p align="center">A feature-rich wiki for minimalists</a></p>
@@ -20,10 +20,10 @@ Getting Started
If you have go
```
go get -u -v github.com/schollz/cowyo
go get -u github.com/schollz/cowyo/...
```
or just download from the [latest releases](https://github.com/schollz/cowyo/releases/latest).
or just [download the latest release](https://github.com/schollz/cowyo/releases/latest).
## Run
@@ -37,7 +37,7 @@ and it will start a server listening on `0.0.0.0:8050`. To view it, just go to
### Running with TLS
Specify a matching pair of SSL Certificate and Key to run cowyo using https. Cowyo will now run in a secure session.
Specify a matching pair of SSL Certificate and Key to run cowyo using https. *cowyo* will now run in a secure session.
*N.B. Let's Encrypt is a CA that signs free and signed certificates.*
@@ -85,6 +85,17 @@ Just like in mission impossible.
![Self-destructing](http://i.imgur.com/upMxFQh.gif)
## Development
You can run the tests using
```
$ cd $GOPATH/src/github.com/schollz/cowyo
$ go test ./...
```
Any contributions are welcome.
## License
MIT

28
encrypt/encrypt.go Normal file
View File

@@ -0,0 +1,28 @@
package encrypt
import (
"crypto/sha256"
"encoding/hex"
"github.com/schollz/cryptopasta"
)
func EncryptString(toEncrypt string, password string) (string, error) {
key := sha256.Sum256([]byte(password))
encrypted, err := cryptopasta.Encrypt([]byte(toEncrypt), &key)
if err != nil {
return "", err
}
return hex.EncodeToString(encrypted), nil
}
func DecryptString(toDecrypt string, password string) (string, error) {
key := sha256.Sum256([]byte(password))
contentData, err := hex.DecodeString(toDecrypt)
if err != nil {
return "", err
}
bDecrypted, err := cryptopasta.Decrypt(contentData, &key)
return string(bDecrypted), err
}

22
encrypt/encrypt_test.go Normal file
View File

@@ -0,0 +1,22 @@
package encrypt
import "testing"
func TestEncryption(t *testing.T) {
s, err := EncryptString("some string", "some password")
if err != nil {
t.Errorf("What")
}
d, err := DecryptString(s, "some wrong password")
if err == nil {
t.Errorf("Should throw error for bad password")
}
d, err = DecryptString(s, "some password")
if err != nil {
t.Errorf("Should not throw password")
}
if d != "some string" {
t.Errorf("Problem decoding")
}
}

View File

@@ -10,6 +10,7 @@ import (
// "github.com/gin-contrib/static"
"github.com/gin-contrib/multitemplate"
"github.com/gin-gonic/gin"
"github.com/schollz/cowyo/encrypt"
)
func serve(host, port, crt_path, key_path string, TLS bool) {
@@ -27,6 +28,7 @@ func serve(host, port, crt_path, key_path string, TLS bool) {
router.GET("/:page/*command", handlePageRequest)
router.POST("/update", handlePageUpdate)
router.POST("/relinquish", handlePageRelinquish) // relinquish returns the page no matter what (and destroys if nessecary)
router.POST("/exists", handlePageExists)
router.POST("/prime", handlePrime)
router.POST("/lock", handleLock)
router.POST("/encrypt", handleEncrypt)
@@ -209,6 +211,26 @@ func handlePageRequest(c *gin.Context) {
})
}
func handlePageExists(c *gin.Context) {
type QueryJSON struct {
Page string `json:"page"`
}
var json QueryJSON
err := c.BindJSON(&json)
if err != nil {
log.Trace(err.Error())
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Wrong JSON", "exists": false})
return
}
p := Open(json.Page)
if len(p.Text.GetCurrent()) > 0 {
c.JSON(http.StatusOK, gin.H{"success": true, "message": json.Page + " found", "exists": true})
} else {
c.JSON(http.StatusOK, gin.H{"success": true, "message": json.Page + " not found", "exists": false})
}
}
func handlePageUpdate(c *gin.Context) {
type QueryJSON struct {
Page string `json:"page"`
@@ -234,6 +256,7 @@ func handlePageUpdate(c *gin.Context) {
log.Trace("Update: %v", json)
p := Open(json.Page)
var message string
success := false
if p.IsLocked {
message = "Locked, must unlock first"
} else if p.IsEncrypted {
@@ -248,8 +271,9 @@ func handlePageUpdate(c *gin.Context) {
}
p.Save()
message = "Saved"
success = true
}
c.JSON(http.StatusOK, gin.H{"success": true, "message": message})
c.JSON(http.StatusOK, gin.H{"success": success, "message": message})
}
func handlePrime(c *gin.Context) {
@@ -328,7 +352,7 @@ func handleEncrypt(c *gin.Context) {
q := Open(json.Page)
var message string
if p.IsEncrypted {
decrypted, err2 := DecryptString(p.Text.GetCurrent(), json.Passphrase)
decrypted, err2 := encrypt.DecryptString(p.Text.GetCurrent(), json.Passphrase)
if err2 != nil {
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Wrong password"})
return
@@ -342,7 +366,7 @@ func handleEncrypt(c *gin.Context) {
message = "Decrypted"
} else {
currentText := p.Text.GetCurrent()
encrypted, _ := EncryptString(currentText, json.Passphrase)
encrypted, _ := encrypt.EncryptString(currentText, json.Passphrase)
q.Erase()
q = Open(json.Page)
q.Update(encrypted)

View File

@@ -17,7 +17,7 @@ func TestListFiles(t *testing.T) {
p.Update("A different bunch of data")
p = Open("testpage3")
p.Update("Not much else")
n, l := DirectoryList()
n, l, _, _ := DirectoryList()
if strings.Join(n, " ") != "testpage testpage2 testpage3" {
t.Errorf("Names: %s, Lengths: %d", n, l)
}

View File

@@ -1,7 +1,6 @@
package main
import (
"crypto/sha256"
"encoding/base32"
"encoding/binary"
"encoding/hex"
@@ -14,7 +13,6 @@ import (
"github.com/jcelliott/lumber"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday"
"github.com/schollz/cryptopasta"
"github.com/shurcooL/github_flavored_markdown"
"golang.org/x/crypto/bcrypt"
)
@@ -160,26 +158,6 @@ func CheckPasswordHash(password, hashedString string) error {
return bcrypt.CompareHashAndPassword(hash, []byte(password))
}
func EncryptString(toEncrypt string, password string) (string, error) {
key := sha256.Sum256([]byte(password))
encrypted, err := cryptopasta.Encrypt([]byte(toEncrypt), &key)
if err != nil {
return "", err
}
return hex.EncodeToString(encrypted), nil
}
func DecryptString(toDecrypt string, password string) (string, error) {
key := sha256.Sum256([]byte(password))
contentData, err := hex.DecodeString(toDecrypt)
if err != nil {
return "", err
}
bDecrypted, err := cryptopasta.Decrypt(contentData, &key)
return string(bDecrypted), err
}
// exists returns whether the given file or directory exists or not
func exists(path string) bool {
_, err := os.Stat(path)

View File

@@ -32,23 +32,3 @@ func TestHashing(t *testing.T) {
t.Errorf("Should NOT be correct password")
}
}
func TestEncryption(t *testing.T) {
s, err := EncryptString("some string", "some password")
if err != nil {
t.Errorf("What")
}
log.Debug(s)
d, err := DecryptString(s, "some wrong password")
if err == nil {
t.Errorf("Should throw error for bad password")
}
d, err = DecryptString(s, "some password")
if err != nil {
t.Errorf("Should not throw password")
}
if d != "some string" {
t.Errorf("Problem decoding")
}
}