Switching from implementing XTEA in JavaScript to Go

I'm attempting to convert the XTEA implementation from JavaScript to Go language:

function decipherXTea(v, k) {
    const ROUNDS = 32;
    const DELTA = 0x9E3779B9;

    let y = v[0];
    let z = v[1];
    let sum = DELTA * ROUNDS;
    for (let round = 0; round < ROUNDS; round++) {
        z -= ((y << 4 ^ y >> 5) + y) ^ (sum + k[sum >> 11 & 3]);
        sum -= DELTA;
        y -= ((z << 4 ^ z >> 5) + z) ^ (sum + k[sum & 3]);
    }
    return [
        y,
        z
    ];
}

Strangely, my Go code is producing identical initial values for y and z, but the final result after completing the rounds does not match what the Go code computes at all.

This is my Go version:

func decrypt(k [4]int64, block []byte) []byte {
    y := int64(int32(block[0])<<24 | int32(block[1])<<16 | int32(block[2])<<8 | int32(block[3])<<0)
    z := int64(int32(block[4])<<24 | int32(block[5])<<16 | int32(block[6])<<8 | int32(block[7])<<0)
    fmt.Printf("y: %d, z: %d\n", y, z)

    const (
        rounds       = 32
        delta  int64 = 0x9E3779B9
    )

    sum := delta * rounds
    for i := 0; i < rounds; i++ {
        z -= int64(int32(int64(int32(y)<<4^int32(y)>>5)+y) ^ int32(sum+k[int32(sum)>>11&3]))
        sum -= delta
        y -= int64((int32(z<<4^z>>5) + int32(z)) ^ int32(sum+k[int32(sum)&3]))
    }

    fmt.Printf("result: %d, %d\n", y, z)

    end := make([]byte, 8)
    end[0] = byte(y >> 24 & 255)
    end[1] = byte(y >> 16 & 255)
    end[2] = byte(y >> 8 & 255)
    end[3] = byte(y & 255)

    end[4] = byte(z >> 24 & 255)
    end[5] = byte(z >> 16 & 255)
    end[6] = byte(z >> 8 & 255)
    end[7] = byte(z & 255)

    return end
}

Answer №1

Understanding and implementing your code with nested integer casts proved to be challenging. However, it appears to yield similar outcomes as golang.org/x/crypto/xtea, which essentially mirrors your solution:

func decrypt(k [4]uint32, block []byte) []byte {
    y := uint32(block[0])<<24 | uint32(block[1])<<16 | uint32(block[2])<<8 | uint32(block[3])
    z := uint32(block[4])<<24 | uint32(block[5])<<16 | uint32(block[6])<<8 | uint32(block[7])

    const (
        rounds        = 32
        delta  uint32 = 0x9E3779B9
    )

    sum := delta
    sum *= rounds
    for i := 0; i < rounds; i++ {
        z -= (y<<4 ^ y>>5 + y) ^ (sum + k[sum>>11&3])
        sum -= delta
        y -= (z<<4 ^ z>>5 + z) ^ (sum + k[sum&3])
    }

    end := make([]byte, 8)
    end[0] = byte(y >> 24)
    end[1] = byte(y >> 16)
    end[2] = byte(y >> 8)
    end[3] = byte(y)

    end[4] = byte(z >> 24)
    end[5] = byte(z >> 16)
    end[6] = byte(z >> 8)
    end[7] = byte(z)

    return end
}

Experiment with it on the Go playground. It's worth noting that the type conversions might introduce errors at certain points. Additionally, it's advised to heed the caution that XTEA is not recommended for use in contemporary applications.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How jQuery manipulates the DOM during a drag-and-drop operation

My current challenge involves implementing jquery-ui sortable on items that appear while scrolling. Below is the code snippet I am using: var gridTop = 0, gridBottom = container.outerHeight(); $('#play-list').on('scroll', ...

Implementing the jQuery datepicker in Angular.js can be challenging

I recently started learning Angular.js and have been working on a web application using this framework. I am now looking to include a datepicker in one of my forms. Below is the code snippet that I have implemented: app.js myapp.directive(& ...

Having trouble extracting the responseText from the Ajax request

My current challenge involves making an ajax call and receiving an Object in the response. However, when I attempt to access "responseText," it keeps returning as undefined: var results = API.get('users', { username: un, userpass: pw } ); conso ...

JavaScript - Declaring canvas as a global object

Presently, I am experimenting with HTML5 and using JavaScript to create a basic 2D Tile map. Progress is going smoothly; however, I have come across an issue where I am unable to contain everything within one large function. I am attempting to make the ca ...

Instructions on how to bring in the Helper class in a React component

I have been working on a helper class in React, and I'm encountering some issues. The setup is shown in the image below: https://i.sstatic.net/4eweA.png In my App.js file, I initially tried importing the Helpers class like this: import Helpers fr ...

Using the typeof operator to test a Typescript array being passed as an object

I have a puzzling query about this particular code snippet. It goes like this: export function parseSomething(someList: string[]): string[] { someList.forEach((someField: string) => { console.log(typeof someField) }) Despite passing a s ...

Sending a text parameter to the JavaScript Executor within a Selenium framework

Although this question has been asked before, I have searched for multiple answers and have not found a solution to my specific problem. if (driver instanceof JavascriptExecutor) { System.out.println("In try"); ((JavascriptExecutor)driver) ...

Can Ember store in Route handle Post requests?

Is there a way to use ember store functionality similar to this.store.findAll('report') for making POST requests with postObj in my route? Also, how should I handle the response received from these requests? Right now, I am sending ajax POST requ ...

Maintaining awareness of which accordion drawer is currently open within a React application

Just getting started with coding, I recently created a collapsible accordion in a NextJs app using the react-collapse package. Everything seems to be working fine, but I'm running into an issue with keeping track of which 'drawer' is current ...

Explanation for the strange floating math in JavaScript - Understanding the IEEE 754 standard for laymen

When it comes to JavaScript and working with floating point numbers, I always feel a bit lost. Dealing with decimals makes me nervous because I'm never quite sure what's happening behind the scenes. If only I understood how the IEEE 754 standard ...

Convert former function to use async and await system

I need to convert this function to async, but I am facing an issue where the users object obtained from mapping interactions is not returning data in the expected format. When I run the async function on the client side, my values are showing up as nil. Th ...

Utilize the Jest moduleNameMapper for locating files: "resolver": undefined

Among the various files I have, there is a text file located in the component directory within the path: src/components/text Despite this, Jest is unable to locate the file when utilizing the webpack alias import Text from "components/text"; I ...

DailyCodingChallenge: Discover two elements in an array that add up to a specified value

As someone who is relatively new to coding, I recently signed up for the daily coding problem mailing list and received the following question: If given a list of numbers and a specific number k, can you determine whether any two numbers from the list a ...

Issue with bidirectional binding on angular material slide toggle not functioning as anticipated (Angular 4)

My angular material slide-toggle implementation seems to be working, but I'm facing an issue where it doesn't bind the value to the relevant variable as expected. // other irrelevant imports above.. import {MatDialog, MatDialogRef, MAT_DIALOG_DA ...

Working with double quotes within variable in JavaScript

I am currently working on dynamically creating HTML tags using JavaScript. Please take a look at the code snippet below: function getPhotosSuccess(data) { if (data.d.results.length > 0) { var response = data.d.results; var innerht ...

How to iterate through the elements of an object within an array using Vue.js and TypeScript

There was an issue with rendering the form due to a TypeError: Cannot read properties of undefined (reading '0'). This error occurred at line 190 in the code for form1.vue. The error is also caught as a promise rejection. Error Occurred <inpu ...

Sending a file through an Ajax POST request to a PHP server

I am attempting to upload the HTML input file to my PHP file using a different method than the traditional synchronous HTML forms. The problem I am encountering is that it seems like I am not correctly POST'ing the input file to my PHP file because t ...

A tutorial on adjusting camera angles through mouse dragging in Three.js

I have created a unique Rolex cube with varying layers along the "Z axis" and I need to adjust the camera position to preview the geometry accurately. How can I change the camera position by dragging the window and also manipulate individual cone rotations ...

What is the best way to remove every other second and third element from an array?

I am looking to remove every second and third element of an array in Javascript. The array I am working with is as follows: var fruits = ["Banana", "yellow", "23", "Orange", "orange", "12", "Apple", "green", "10"]; My goal is to delete every second and ...

What is the best way to insert a new item into an array that is nested within an object?

Currently, I am delving into the world of using $resource in angularjs and finding great examples in this answer AngularJS $resource RESTful example. Fetching and creating records is working fine, but now my challenge lies in adding a "section" to an exist ...