summaryrefslogtreecommitdiff
path: root/imagestore/hash.go
diff options
context:
space:
mode:
authorhorus_arch2015-04-19 22:09:52 +0200
committerhorus_arch2015-04-19 22:09:52 +0200
commit01e9a34952bd6ddd383680b0ca2312e476ad07a6 (patch)
tree00902575e5c271cc5d35ea65aa8795b8caeb97bc /imagestore/hash.go
downloadmandible-01e9a34952bd6ddd383680b0ca2312e476ad07a6.tar.gz
Initial commit.
Diffstat (limited to 'imagestore/hash.go')
-rw-r--r--imagestore/hash.go76
1 files changed, 76 insertions, 0 deletions
diff --git a/imagestore/hash.go b/imagestore/hash.go
new file mode 100644
index 0000000..d24e6fc
--- /dev/null
+++ b/imagestore/hash.go
@@ -0,0 +1,76 @@
+package imagestore
+
+import (
+ "crypto/rand"
+ "log"
+)
+
+type HashGenerator struct {
+ hashGetter chan string
+ length int
+ store ImageStore
+}
+
+func (this *HashGenerator) init() {
+ go func() {
+ storeObj := &StoreObject{
+ "",
+ "",
+ "original",
+ "",
+ }
+
+ for {
+ str := ""
+
+ for len(str) < this.length {
+ c := 10
+ bArr := make([]byte, c)
+ _, err := rand.Read(bArr)
+ if err != nil {
+ log.Println("error:", err)
+ break
+ }
+
+ for _, b := range bArr {
+ if len(str) == this.length {
+ break
+ }
+
+ /**
+ * Each byte will be in [0, 256), but we only care about:
+ *
+ * [48, 57] 0-9
+ * [65, 90] A-Z
+ * [97, 122] a-z
+ *
+ * Which means that the highest bit will always be zero, since the last byte with high bit
+ * zero is 01111111 = 127 which is higher than 122. Lower our odds of having to re-roll a byte by
+ * dividing by two (right bit shift of 1).
+ */
+
+ b = b >> 1
+
+ // The byte is any of 0-9 A-Z a-z
+ byteIsAllowable := (b >= 48 && b <= 57) || (b >= 65 && b <= 90) || (b >= 97 && b <= 122)
+
+ if byteIsAllowable {
+ str += string(b)
+ }
+ }
+
+ }
+
+ storeObj.Name = str
+
+ exists, _ := this.store.Exists(storeObj)
+ if !exists {
+ this.hashGetter <- str
+ }
+ }
+ }()
+}
+
+func (this *HashGenerator) Get() string {
+ return <-this.hashGetter
+}