Esempio n. 1
0
func FrequencyAttack(chars []string, charset, cipher string) (guesses []string, err error) {
	//
	for _, commonChar := range frequency.Top3 {
		for _, char := range chars {
			pos := strings.Index(charset, char)
			if pos == -1 {
				return nil, errorsutil.Errorf("Character `%c` is not present in charset: `%s`", char, charset)
			}

			commonCharPos := strings.Index(charset, commonChar)
			if commonCharPos == -1 {
				return nil, errorsutil.Errorf("Character `%c` is not present in charset: `%s`", commonChar, charset)
			}

			unwindBy := commonCharPos - pos
			unciphered, err := broken.Caesar(charset, cipher, unwindBy)
			if err != nil {
				return nil, err
			}

			guesses = append(guesses, unciphered)
		}

	}

	return guesses, nil
}
Esempio n. 2
0
// Returns a unit struct initiated with the raw codes
func NewUnit(baseRawCode, customRawCode string) (unit *Unit, err error) {
	if temp.IsRawCode(baseRawCode) == false {
		return nil, errorsutil.Errorf("Invalid base unit rawcode: %s - must be upper or lowercase alpha numeric and 4 characters long", baseRawCode)
	} else if temp.IsRawCode(customRawCode) == false {
		return nil, errorsutil.Errorf("Invalid custom unit rawcode: %s - must be upper or lowercase alpha numeric and 4 characters long", customRawCode)
	}

	return &Unit{BaseRawCode: baseRawCode, CustomRawCode: customRawCode}, nil
}
Esempio n. 3
0
// Caesar shifts each character in the message some fixed number of
// positions down the charset.
func Caesar(charset, message string, shiftBy int) (cipher string, err error) {
	// When shift overflows the slice index we use the modulo of the
	// charset length to simulate rotation.
	// Example:
	// When Y + 2 becomes A; (24 + 2) % 26 = 0		-> charset[0]	= 'a'
	// Or when A - 1 becomes Z; (26 - 1) % 26 = 25	-> charset[25]	= 'z'
	csLen := len(charset)

	// For each character in message
	// Get it's position in the charset
	// The new character is it's position in the charset + shiftBy
	// Simulate rotation with modulo and subtraction.
	for _, mChar := range message {
		pos := strings.IndexRune(charset, mChar)
		if pos == -1 {
			return "", errorsutil.Errorf("Character `%c` is not present in charset: `%s`", mChar, charset)
		}

		posShift := pos + shiftBy
		if posShift < 0 {
			// Example:
			// 26 + (-1) = 25; charset[25] = 'z'
			posShift = csLen + posShift
		}

		// Concatenate the final cipher string
		cipher += string(charset[posShift%csLen])
	}

	return cipher, nil
}
Esempio n. 4
0
File: w3u.go Progetto: karlek/sensou
func GetRawCode(buffer *bytes.Buffer) (rawCode string, err error) {
	rawCode = string(buffer.Next(4))
	if !temp.IsRawCode(rawCode) {
		return "", errorsutil.Errorf("Illegal raw code: [% X]", rawCode)
	}

	return rawCode, nil
}
Esempio n. 5
0
// Adds a new field to the unit
func (unit *Unit) AddField(fieldRawCode string, value interface{}) (err error) {
	if temp.IsRawCode(fieldRawCode) == false {
		return errorsutil.Errorf("Invalid field rawcode: %s - must be upper or lowercase alpha numeric and 4 characters long", fieldRawCode)
	}

	unit.Fields = append(unit.Fields, field.Field{Name: fieldRawCode, Value: value})
	return nil
}
Esempio n. 6
0
func FromBuf(byteNum []byte) (b Binary, err error) {

	// buf := bytes.NewBuffer([]byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40})

	if len(byteNum)%8 != 1 {
		for {
			if len(byteNum)%8 == 0 {
				break
			}
			byteNum = append(byteNum, 0x00)
		}
	}

	buf := bytes.NewBuffer(byteNum)

	err = binary.Read(buf, binary.LittleEndian, &b)
	if err != nil {
		return 0, errorsutil.Errorf("FromBuf: %s - % x", err, byteNum)
	}

	return b, nil
}
Esempio n. 7
0
File: w3u.go Progetto: karlek/sensou
func Open(fileName string) (w3 *W3u, err error) {

	w3 = New()

	// Read w3u file
	file, err := os.Open(fileName)
	if err != nil {
		return nil, err
	}

	// Read all bytes from the file pointer
	buf, err := ioutil.ReadAll(file)
	if err != nil {
		return nil, err
	}

	// Make the bytes into a Buffer
	buffer := bytes.NewBuffer(buf)

	// Check header
	w3.Header = buffer.Next(8)
	if !w3.IsHeader() {
		return nil, errorsutil.Errorf("Invalid w3u header! Correct header: % X, your header: % X", Header, w3.Header)
	}

	// Number of units
	numUnits := uint(buffer.Next(1)[0])
	if numUnits < 0 {
		return nil, errorsutil.Errorf("Invalid number of units: %d\n", numUnits)
	}

	// Skip seperator
	buffer.Next(3)

	for i := 0; i < int(numUnits); i++ {

		// Base unit rawcode
		baseUnit, err := GetRawCode(buffer)
		if err != nil {
			return nil, err
		}

		// Custom unit rawcode
		customUnit, err := GetRawCode(buffer)
		if err != nil {
			return nil, err
		}

		u, err := unit.NewUnit(baseUnit, customUnit)
		if err != nil {
			return nil, err
		}

		// Number of fields
		numFields := uint(buffer.Next(1)[0])
		if numFields < 0 {
			return nil, errorsutil.Errorf("Invalid number of fields: %d\n", numFields)
		}

		// Skip seperator
		buffer.Next(3)

		for j := 0; j < int(numFields); j++ {

			f := new(field.Field)

			// Field name in raw data
			fieldName, err := GetRawCode(buffer)
			if err != nil {
				return nil, err
			}
			f.Name = fieldName

			// Type of field, 0:int(and bool), 1:fixed decimal point, 2:float, 3:string
			fieldType := int(buffer.Next(1)[0])

			// 0, 1, 2, 3 are valid field types
			if fieldType < 0 || fieldType > 3 {
				return nil, errorsutil.Errorf("Invalid field type: %d\n", fieldType)
			}

			// Skip seperator
			buffer.Next(3)

			// ###Add fixed
			switch fieldType {
			case 0:
				f.Value, err = temp.FromBuf(buffer.Next(3))
				if err != nil {
					return nil, err
				}
				// f.Value = temp.BigToLittle(buffer.Next(3))

			case 2:
				// Float value
				f.Value = buffer.Next(3)

			case 3:
				// Get next seperator, that's were the string ends
				strLen := bytes.Index(buffer.Bytes(), []byte{00, 00, 00})

				// String value
				f.Value = string(buffer.Next(strLen))
			}

			// Skip field seperator
			buffer.Next(5)
			u.Fields = append(u.Fields, *f)
		}
		w3.Units = append(w3.Units, *u)
	}

	return w3, nil
}