Example #1
0
func getScramble(keyA *big.Int, keyB *big.Int) *big.Int {
	// keyA:A client public ephemeral values
	// keyB:B server public ephemeral values

	sha1 := sha1.New()
	sha1.Write(pad(keyA))
	sha1.Write(pad(keyB))

	return bytesToBig(sha1.Sum(nil))
}
Example #2
0
File: shax.go Project: ra/go-samp
func main() {
	fmt.Println("Start...")
	// String from:
	// http://en.wikipedia.org/wiki/WebSocket
	s := "x3JJHMbDL1EzLkh9GBhXDw==258EAFA5-E914-47DA-95CA-C5AB0DC85B11"

	// sha1 stuff ...
	sha1 := sha1.New()
	sha1.Write([]byte(s))
	ss := fmt.Sprintf("%x", sha1.Sum(nil))
	fmt.Printf("%s\n", ss)
	w := "1d29ab734b0c9585240069a6e4e3e91b61da1969"
	fmt.Printf("%s\n", w)
	if ss != w {
		panic("Uh oh, something is not right")
	}
	// ---------------------------------------------------------------------------
	// The base64 encoding part of that post is left as an exercise for now.
	// From the article the base64 result should be:
	// HSmrc0sMlYUkAGmm5OPpG2HaGWk=
	// *not tested*
	// ---------------------------------------------------------------------------
	// sha256 stuff ...
	sha256 := sha256.New()
	sha256.Write([]byte(s))
	ss = fmt.Sprintf("%x", sha256.Sum(nil))
	fmt.Printf("%s\n", ss)
	fmt.Println("End...")
}
Example #3
0
// Calculate the strong checksum of a directory.
func CalcStrong(dir Dir) string {
	var sha1 = sha1.New()
	//	s := reprDir(dir)
	//	fmt.Printf("%s\n", s)
	//	sha1.Write(s)
	sha1.Write(reprDir(dir))
	return toHexString(sha1)
}
Example #4
0
// Build a hierarchical tree model representing a file's contents
func IndexFile(path string) (file *File, err error) {
	var f *os.File
	var buf [BLOCKSIZE]byte

	stat, err := os.Stat(path)
	if stat == nil {
		return nil, err
	} else if !stat.IsRegular() {
		return nil, errors.New(fmt.Sprintf("%s: not a regular file", path))
	}

	f, err = os.Open(path)
	if f == nil {
		return nil, err
	}
	defer f.Close()

	file = new(File)
	_, basename := filepath.Split(path)
	file.name = basename
	file.mode = stat.Mode

	if fileInfo, err := f.Stat(); fileInfo != nil {
		file.Size = fileInfo.Size
	} else {
		return nil, err
	}

	var block *Block
	sha1 := sha1.New()
	blockNum := 0

	for {
		switch rd, err := f.Read(buf[:]); true {
		case rd < 0:
			return nil, err
		case rd == 0:
			file.strong = toHexString(sha1)
			return file, nil
		case rd > 0:
			// Update block hashes
			block = IndexBlock(buf[0:rd])
			block.position = blockNum
			file.Blocks = append(file.Blocks, block)

			// update file hash
			sha1.Write(buf[0:rd])

			// Increment block counter
			blockNum++
		}
	}

	return nil, nil
}
Example #5
0
func b64sha1(data []byte) string {
	sha1 := sha1.New()
	sha1.Write(data)
	hash := sha1.Sum(nil)

	b := new(bytes.Buffer)
	b64 := base64.NewEncoder(base64.URLEncoding, b)
	b64.Write(hash)

	return b.String()
}
Example #6
0
// Generate a unique id as a base64 encoded string
func generateId() string {

	// Generate a random 128 bit Id
	z, err := rand.Int(rand.Reader, maxSessionId)
	if err != nil {
		log.Fatal("Cannot generate session id", err)
	}

	// Take the hash of the random id
	sha1 := sha1.New()
	sha1.Write(z.Bytes())

	return base64.StdEncoding.EncodeToString(sha1.Sum([]byte{}))
}
Example #7
0
// Build a hierarchical tree model representing a file's contents
func IndexFile(path string) (fileInfo *FileInfo, blocksInfo []*BlockInfo, err os.Error) {
	var f *os.File
	var buf [BLOCKSIZE]byte

	stat, err := os.Stat(path)
	if stat == nil {
		return nil, nil, err
	} else if !stat.IsRegular() {
		return nil, nil, os.NewError(fmt.Sprintf("%s: not a regular file", path))
	}

	f, err = os.Open(path)
	if f == nil {
		return nil, nil, err
	}
	defer f.Close()

	_, basename := filepath.Split(path)
	fileInfo = &FileInfo{
		Name: basename,
		Mode: stat.Mode,
		Size: stat.Size}

	var block *BlockInfo
	sha1 := sha1.New()
	blockNum := 0
	blocksInfo = []*BlockInfo{}

	for {
		switch rd, err := f.Read(buf[:]); true {
		case rd < 0:
			return nil, nil, err
		case rd == 0:
			fileInfo.Strong = toHexString(sha1)
			return fileInfo, blocksInfo, nil
		case rd > 0:
			// Update block hashes
			block = IndexBlock(buf[0:rd])
			block.Position = blockNum
			blocksInfo = append(blocksInfo, block)

			// update file hash
			sha1.Write(buf[0:rd])

			// Increment block counter
			blockNum++
		}
	}
	panic("Impossible")
}
Example #8
0
func MojangSha1Hex(arrBytes ...[]byte) (val string) {
	sha1 := sha1.New()
	for _, bytes := range arrBytes {
		sha1.Write(bytes)
	}
	hash := sha1.Sum(nil)
	negative := (hash[0] & 0x80) == 0x80
	if negative {
		twosCompliment(hash)
	}
	val = hex.EncodeToString(hash)
	val = strings.TrimLeft(val, "0")
	if negative {
		val = "-" + val
	}
	return
}
Example #9
0
func MojangSha1Hex(arrBytes ...[]byte) string {
	sha1 := sha1.New()
	for _, bytes := range arrBytes {
		sha1.Write(bytes)
	}
	hash := sha1.Sum(nil)
	negative := (hash[0] & 0x80) == 0x80
	if negative {
		twosCompliment(hash)
	}
	hexString := hex.EncodeToString(hash)
	hexString = strings.TrimLeft(hexString, "0")
	if negative {
		hexString = "-" + hexString
	}
	return hexString
}
Example #10
0
func calcFileInfo(fi *FileInfo) {
	fmt.Printf("calcFileInfo: '%s'\n", fi.Path)
	var buf [16 * 1024]byte
	r, err := os.Open(fi.Path)
	fataliferr(err)
	defer r.Close()
	sha1 := sha1.New()
	md5Hash := md5.New()
	fi.Size = 0
	for {
		n, err := r.Read(buf[:])
		if err == io.EOF {
			break
		}
		d := buf[:n]
		fataliferr(err)
		fatalif(n == 0, "n is 0")
		fi.Size += int64(n)
		_, err = sha1.Write(d)
		fataliferr(err)
		_, err = md5Hash.Write(d)
		fataliferr(err)
	}
	sha1Sum := sha1.Sum(nil)
	fi.Sha1Hex = fmt.Sprintf("%x", sha1Sum)

	md5Sum := md5Hash.Sum(nil)
	fi.Md5Hex = fmt.Sprintf("%x", md5Sum)
	fi.Md5B64 = base64.StdEncoding.EncodeToString(md5Sum)

	fi.S3PathSha1Part = sha1HexToS3Path(fi.Sha1Hex)
	ext := strings.ToLower(filepath.Ext(fi.Path))
	fi.S3FullPath = s3Dir + fi.S3PathSha1Part + ext
	fmt.Printf("  sha1: %s\n", fi.Sha1Hex)
	fmt.Printf("   md5: %s\n", fi.Md5Hex)
	fmt.Printf("   md5: %s\n", fi.Md5B64)
	fmt.Printf("    s3: %s\n", fi.S3FullPath)
	fmt.Printf("  size: %d\n", fi.Size)
}
Example #11
0
func getClientProof(user string, password string, salt []byte, keyA *big.Int, keyB *big.Int, keya *big.Int) (keyM []byte, keyK []byte) {
	// M = H(H(N) xor H(g), H(I), s, A, B, K)
	prime, g, _ := getPrime()
	keyK = getClientSession(user, password, salt, keyA, keyB, keya)

	n1 := bytesToBig(bigToSha1(prime))
	n2 := bytesToBig(bigToSha1(g))
	n3 := mathutil.ModPowBigInt(n1, n2, prime)
	n4 := getStringHash(user)
	sha1 := sha1.New()
	sha1.Write(n3.Bytes())
	sha1.Write(n4.Bytes())
	sha1.Write(salt)
	sha1.Write(keyA.Bytes())
	sha1.Write(keyB.Bytes())
	sha1.Write(keyK)
	keyM = sha1.Sum(nil)

	return keyM, keyK
}
Example #12
0
func Sha1Hex(str string) (val string) {
	sha1 := sha1.New()
	sha1.Write([]byte(str))
	val = hex.EncodeToString(sha1.Sum(nil))
	return
}
Example #13
0
func main() {
	flag.Parse()
	sha1 := sha1.New()
	sha1.Write([]byte(flag.Arg(0)))
	fmt.Printf("%x\n", sha1.Sum(nil))
}
Example #14
0
File: rid.go Project: holygeek/rid
func getSha1Str(reposig string) string {
	sha1 := sha1.New()
	sha1.Write([]byte(reposig))
	return fmt.Sprintf("%x", sha1.Sum(nil))
}
Example #15
0
func bigToSha1(n *big.Int) []byte {
	sha1 := sha1.New()
	sha1.Write(n.Bytes())

	return sha1.Sum(nil)
}
Example #16
0
func GetHash(blob *[]byte) string {
	sha1 := sha1.New()
	sha1.Write(*blob)
	return fmt.Sprintf("sha1-%x", sha1.Sum())
}
Example #17
0
func Sha1Hex(str string) string {
	sha1 := sha1.New()
	sha1.Write([]byte(str))
	return hex.EncodeToString(sha1.Sum(nil))
}
Example #18
0
// isValid validates the checksum of a block.
func (b block) isValid() bool {
	sha1 := sha1.New()
	sha1.Write(b.Data[:b.Hdr.Size])
	return bytes.Equal(b.Hdr.Score[:], sha1.Sum(nil))
}
Example #19
0
// Strong checksum algorithm used throughout replican
// For now, it's SHA-1.
func StrongChecksum(buf []byte) string {
	var sha1 = sha1.New()
	sha1.Write(buf)
	return toHexString(sha1)
}
Example #20
0
func calcFileInfo(fi *FileInfo) {
	fmt.Printf("calcFileInfo: '%s'\n", fi.Path)
	const BufSize = 16 * 1024
	var buf [BufSize]byte
	r, err := os.Open(fi.Path)
	fataliferr(err)
	defer r.Close()
	sha1 := sha1.New()
	md5Hash := md5.New()
	fi.ShouldCompress = false
	tryCompressFirsBlock := shouldTryCompressFile(fi.Path)
	var gzw *gzip.Writer
	compressedData := &bytes.Buffer{}
	fi.Size = 0
	fi.CompressedData = nil
	for {
		n, err := r.Read(buf[:])
		if err == io.EOF {
			break
		}
		d := buf[:n]
		fataliferr(err)
		fatalif(n == 0, "n is 0")
		fi.Size += n
		_, err = sha1.Write(d)
		fataliferr(err)
		_, err = md5Hash.Write(d)
		fataliferr(err)
		if tryCompressFirsBlock {
			tryCompressFirsBlock = false
			gz, err := gzip.NewWriterLevel(compressedData, gzip.BestCompression)
			fataliferr(err)
			_, err = gz.Write(d)
			fataliferr(err)
			gz.Close()
			compressedSize := compressedData.Len()
			saved := n - compressedSize
			// relatively high threshold of 20% savings on compression
			fi.ShouldCompress = saved > 0 && perc(compressedSize, saved) > 20
			diff := n - compressedSize
			fmt.Printf("  should compress: %v, %d => %d (%d %.2f%%)\n", fi.ShouldCompress, n, compressedSize, diff, perc(n, diff))
			if fi.ShouldCompress {
				compressedData = &bytes.Buffer{}
				gzw, err = gzip.NewWriterLevel(compressedData, gzip.BestCompression)
				fataliferr(err)
			}
		}
		if gzw != nil {
			_, err = gzw.Write(d)
			fataliferr(err)
		}
	}
	sha1Sum := sha1.Sum(nil)
	fi.Sha1Hex = fmt.Sprintf("%x", sha1Sum)

	if gzw != nil {
		gzw.Close()
		compressedSize := compressedData.Len()
		// only use compressed if compressed by at least 5%
		if compressedSize+(compressedSize/20) < fi.Size {
			fi.CompressedData = compressedData.Bytes()
		}
	}

	md5Sum := md5Hash.Sum(nil)
	fi.Md5Hex = fmt.Sprintf("%x", md5Sum)
	// if compressed, md5 is of the compressed content
	if fi.CompressedData != nil {
		md5Sum2 := md5.Sum(fi.CompressedData)
		fi.Md5Hex = fmt.Sprintf("%x", md5Sum2[:])
	}

	fi.S3PathSha1Part = sha1HexToS3Path(fi.Sha1Hex)
	ext := strings.ToLower(filepath.Ext(fi.Path))
	if fi.CompressedData != nil {
		fi.S3FullPath = fi.S3PathSha1Part + ".gz" + ext
	} else {
		fi.S3FullPath = fi.S3PathSha1Part + ext
	}
	fmt.Printf("  sha1: %s\n", fi.Sha1Hex)
	fmt.Printf("   md5: %s\n", fi.Md5Hex)
	fmt.Printf("    s3: %s\n", fi.S3FullPath)
	fmt.Printf("  size: %d\n", fi.Size)
	if fi.CompressedData != nil {
		sizedCompressed := len(fi.CompressedData)
		saved := fi.Size - sizedCompressed
		fmt.Printf("  size compressed: %d (saves %d %.2f%%)\n", sizedCompressed, saved, perc(fi.Size, saved))
	}
}
Example #21
0
// Calculate the strong checksum of a directory.
func (dir *Dir) calcStrong() string {
	var sha1 = sha1.New()
	sha1.Write(dir.stringBytes())
	return toHexString(sha1)
}