示例#1
0
func (x *DNSTransportDownstreamCodec) Encode(msg []byte, header DNSCodecHeader) string {
	header_bytes := x.header_codec.EncodeToBytes(&header)

	dst := make([]byte, ascii85.MaxEncodedLen(4+len(msg)))

	ret_len := ascii85.Encode(dst[0:5], header_bytes[:])
	ret_len += ascii85.Encode(dst[5:], msg)

	return string(dst[:ret_len])
}
示例#2
0
func (e *Engine) ascii85() error {
	b := e.stack.Pop()
	enc := make([]byte, ascii85.MaxEncodedLen(len(b)))
	sz := ascii85.Encode(enc, b)
	e.stack.Push(enc[0:sz])
	return nil
}
示例#3
0
func poplop(master string, n Scheme) (string, error) {
	h := sha512.New()
	io.WriteString(h, master)
	io.WriteString(h, n.Name)
	if n.Counter > 0 {
		io.WriteString(h, strconv.Itoa(n.Counter))
	}
	hash := h.Sum(nil)
	dst := make([]byte, ascii85.MaxEncodedLen(len(hash)))
	ascii85.Encode(dst, hash)

	str := string(dst)
	if n.Forbidden != "" {
		str = strings.Map(n.mapping(), str)
	}

	length := 20
	if n.MaxLength > 0 {
		length = n.MaxLength
	}

	if len(str) < length {
		return "", errors.New("hash not long enough " + str)
	}
	return str[:length], nil
}
示例#4
0
func main() {
	bpasswd := make([]byte, 16)
	_, e := io.ReadFull(rand.Reader, bpasswd)
	if e != nil {
		fmt.Println(e)
	}

	p1 := base32.StdEncoding.EncodeToString(bpasswd)
	p2 := base64.StdEncoding.EncodeToString(bpasswd)

	encoded := make([]byte, 32)
	l := ascii85.Encode(encoded, bpasswd)

	fmt.Printf("%x\n%s\n%s\n%s\n", bpasswd, p1, p2, encoded[:l])
	fmt.Println(os.Args)
}
示例#5
0
文件: uuid.go 项目: jjenkins/heartkit
// A85 returns ASCII 85 encoded string representation of UUID
func (id *KUUID) A85() string {
	if id.slc == nil || len(id.slc) == 0 {
		return ""
	}
	if UUIDOptions.Cache && id.a85 != "" {
		return id.a85
	}
	bytes := make([]byte, 20)
	ascii85.Encode(bytes, id.slc)
	if UUIDOptions.WrapA85 {
		parts := []string{"<~", string(bytes[:]), "~>"}
		id.a85 = strings.Join(parts, "")
	} else {
		id.a85 = string(bytes)
	}
	return id.a85
}
示例#6
0
func insertAssets(w io.Writer, fc *fileCollection, c *config) (err error) {
	// NewAsset(name string, content []byte, size int64, mode os.FileMode, modTime time.Time) *Asset

	_, err = fmt.Fprint(w, "\n\tvar assets = map[string]magpie.Asset{\n")
	if err != nil {
		return
	}
	for _, a := range fc.assets {
		fmt.Printf("Hash length:%v\n", a.hash)
		hash := make([]byte, ascii85.MaxEncodedLen(len(a.hash)))
		l := ascii85.Encode(hash, a.hash)
		_, err = fmt.Fprintf(w, "\t\t%q: magpie.NewAsset(%q, read(_%s), %d, os.FileMode(%d), time.Unix(%d, 0), %q),\n", a.name, a.name, a.constant, a.info.Size(), a.info.Mode(), a.info.ModTime().Unix(), string(hash[:l]))
		if err != nil {
			return
		}

	}
	_, err = fmt.Fprint(w, "\n\t}\n")
	return
}
示例#7
0
文件: main.go 项目: reusee/memory
func main() {
	// lock
	ln, err := net.Listen("tcp", "127.0.0.1:61297")
	if err != nil {
		fmt.Printf("lock failed.\n")
		return
	}
	defer ln.Close()

	mem := &Memory{
		Concepts: make(map[string]*Concept),
		Connects: make(map[string]*Connect),
	}
	mem.Load()

	getPendingConnect := func(now time.Time) []*Connect {
		var connects []*Connect
		for _, connect := range mem.Connects {
			from := mem.Concepts[connect.From]
			if (from.What == WORD || from.What == SENTENCE) && from.Incomplete {
				continue
			}

			lastHistory := connect.Histories[len(connect.Histories)-1]
			if lastHistory.Time.Add(LevelTime[lastHistory.Level]).After(now) {
				continue
			}

			connects = append(connects, connect)
		}
		return connects
	}

	var cmd string
	if len(os.Args) == 1 {
		cmd = "train"
	} else {
		cmd = os.Args[1]
	}
	max := 22
	if len(os.Args) == 2 && regexp.MustCompile(`[0-9]+`).MatchString(os.Args[1]) {
		max, _ = strconv.Atoi(os.Args[1])
		cmd = "train"
	}

	// add audio files
	if cmd == "add" {
		if len(os.Args) < 3 {
			fmt.Printf("word or sentence?\n")
			os.Exit(0)
		}
		t := os.Args[2]
		var what int
		if strings.HasPrefix(t, "w") {
			what = WORD
		} else if strings.HasPrefix(t, "s") {
			what = SENTENCE
		} else {
			fmt.Printf("unknown type\n")
			os.Exit(0)
		}

		p := filepath.Join(rootPath, "files")
		fmt.Printf("files directory: %s\n", p)

		hasher := sha512.New()
		for _, f := range os.Args[2:] {
			data, err := ioutil.ReadFile(f)
			if err != nil {
				continue
			}
			f, _ = filepath.Abs(f)
			f = strings.TrimPrefix(f, p)
			hasher.Reset()
			hasher.Write(data)
			buf := make([]byte, ascii85.MaxEncodedLen(sha512.Size))
			l := ascii85.Encode(buf, hasher.Sum(nil))

			concept := &Concept{
				What:     AUDIO,
				File:     f,
				FileHash: string(buf[:l]),
			}
			exists := mem.AddConcept(concept)
			if exists {
				fmt.Printf("skip %s\n", f)
			} else {
				fmt.Printf("add %s %s\n", f, concept.FileHash)
				// add text concept
				textConcept := &Concept{
					What:       what,
					Incomplete: true,
					Serial:     mem.NextSerial(),
				}
				mem.AddConcept(textConcept)
				// add connect
				mem.AddConnect(&Connect{
					From: concept.Key(),
					To:   textConcept.Key(),
					Histories: []History{
						{Level: 0, Time: time.Now()},
					},
				})
				if what == WORD {
					mem.AddConnect(&Connect{
						From: textConcept.Key(),
						To:   concept.Key(),
						Histories: []History{
							{Level: 0, Time: time.Now()},
						},
					})
				}
			}
		}

		// show stat
		fmt.Printf("%d concepts, %d connects\n", len(mem.Concepts), len(mem.Connects))
		mem.Save()

		// train
	} else if cmd == "train" {
		connects := getPendingConnect(time.Now())
		// sort
		sort.Sort(ConnectSorter{connects, mem})
		if len(connects) > max {
			connects = connects[:max]
		}
		// ui
		//ui_qt(connects, mem)
		ui_gtk(connects, mem)

		// complete connection
	} else if cmd == "complete" {
		for _, connect := range mem.Connects {
			from := mem.Concepts[connect.From]
			if !from.Incomplete && from.Text != "" {
				continue
			}
			if from.What != WORD {
				continue
			}
			to := mem.Concepts[connect.To]
			fmt.Printf("%s\n", to.File)
			to.Play()
			fmt.Scanf("%s\n", &from.Text)
			if from.Text == "" {
				continue
			}
			from.Incomplete = false
			mem.Save()
		}

		// train history
	} else if cmd == "history" {
		counter := make(map[string]int)
		for _, connect := range mem.Connects {
			for _, entry := range connect.Histories {
				t := entry.Time
				counter[fmt.Sprintf("%04d-%02d-%02d", t.Year(), t.Month(), t.Day())]++
			}
		}
		var dates []string
		for date := range counter {
			dates = append(dates, date)
		}
		sort.Strings(dates)
		for _, date := range dates {
			fmt.Printf("%s %d\n", date, counter[date])
		}
	}

}