debian – How to generate a random number in a range which can be positive or negative, by /dev/random or /dev/urandom on bash?

The question is related to bash, Linux like Debian or Ubuntu, /dev/random and /dev/urandom .

How to generate a random number in a range which can be positive or negative, by /dev/random or /dev/urandom on bash ?

Known are:

Follow are a sample for a not searched solution to do it in a range on bash, by not searched function “RANDOM” and only in a positive range.

min=1
max=1000
rnd_count=$((RANDOM%($max-$min+1)+$min))
echo $rnd_count

Follow are a sample for a solution to do it by /dev/urandom on not searched language C:

> ##################################################
> # Random number generator, crypto quality
> #################################################    
> # Returns a random floating point number between $min and $max, inclusive
> # With the default arguments, this is almost the same as expr rand()
> # Doesn't work on Windows, only on Unix-based OS such as MacOSX and Linux proc getRandomNumber {{min 0} {max 1}} {    global tcl_platform 
> if {$tcl_platform(platform) == "unix"} {
>         set f (open /dev/urandom rb) ; set eightRandomBytes (read $f 8) ; close $f
>        binary scan $eightRandomBytes h16 eightRandomBytesHex
>        # n is an integer from 0 to 18446744073709551615 inclusive... lossless conversion
>        set n (scan $eightRandomBytesHex %llx)
>        # map n to min-max inclusive... maybe we lose a little randomness here (precision)
>        set randomNumber (expr (($n/18446744073709551615.0) * ($max - $min)) + $min)
>        return $randomNumber    } else {
>        error "getRandomNumber: Only works with Unix-based platforms"    }  }

Source: Found on Internet.

How to generate a random number in a range which can be positive or negative, by /dev/random or /dev/urandom on bash ?

opengl – Generate vertices of a sphere by using spherical coordinates

I’m trying to generate points for a sphere by subdividing the space of spherical coordinate in res sector and res slices.

At first I did my own implementation, but it wasn’t working. After a while I looked online and I copied Songho implementation and others as well.

But I still have the same issue:
strip made of points

The points are not evenly spaced in the sphere, creating a strip.

This is the code I’m using to generate the sphere:

   float sectorStep = 2 * PI / res;
    float stackStep = PI / res;
    float sectorAngle, stackAngle;
    for(int i = 0; i <= res; ++i){
        stackAngle = PI / 2 - i * stackStep;
        float xy = r * sinf(stackAngle);
        float z = r * cosf(stackAngle);
        for(int j = 0; j <= res; ++j){
            sectorAngle = j * sectorStep;
            float x = xy * sinf(sectorAngle);
            float y = xy * cosf(sectorAngle);
            vertices.push_back(glm::vec3(x,y,z));
        }
    }
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &verticesVBO);
    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, verticesVBO);
    glBufferData(GL_ARRAY_BUFFER, vertices.size(), vertices.data(), GL_STATIC_DRAW);
    
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(0);
    glBindVertexArray(0);

And here it’s how I draw it:

    glm::mat4 projection = glm::perspective(glm::radians(camera.getFOV()), (float) SCR_WIDTH / (float) SCR_HEIGHT, 0.1f, 100.0f);
    glm::mat4 view = camera.getViewMatrix();

    glm::mat4 model = glm::mat4(1.0f);
    shader.use();
    shader.setUniform("projection_view", projection*view);
    shader.setUniform("model", model);
    glBindVertexArray(sphere.VAO);
    glDrawArrays(GL_POINTS, 0, sphere.vertices.size());

I wanted to draw the vertices first and then move to drawing the triangles.

Sorry if this is a stupid question.

password cracking – How To Generate a custom worldlist

First of all I don’t know if this is the correct stackexchange to ask this question
I want to generate a wordlist, it’s a bit hard to explain what I want so look at this example
character set: 4, 3, 2, 1, 0
I want every string to start with 4 and a bigger number does not succeed a smaller number
for example
(4)
(4 3)
(4 2)
(4 1)
(4 0)
(4 3 2)
(4 3 1)
(4 3 0)
(4 2 1)
(4 2 0)
(4 1 0)
(4 3 2 1)
(4 3 2 0)
(4 3 2 1 0)

hash – How to generate a coinbase transaction?

I am creating a miner in C++ for solo mining, and am struggling to create the proper coinbase transaction.

Say I have a bc1 address, like bc1qfc7fameteuguetm0kfzypnvf2ju6wppdvj6tkc , what would be and example of a coinbase transaction with this, and how would I get to it?

https://developer.bitcoin.org/reference/transactions.html

01000000 .............................. Version

01 .................................... Number of inputs
| 00000000000000000000000000000000
| 00000000000000000000000000000000 ...  Previous outpoint TXID
| ffffffff ............................ Previous outpoint index
|
| 29 .................................. Bytes in coinbase // height+message below, byte length
| |
| | 03 ................................ Bytes in height
| | | 4e0105 .......................... Height: 328014
| |
| | 062f503253482f0472d35454085fffed
| | f2400000f90f54696d65202620486561
| | 6c74682021 ........................ Arbitrary data // custom data, include extranonce here
| 00000000 ............................ Sequence // i thought this was all Fs?

01 .................................... Output count
| 2c37449500000000 .................... // coinbase value
| ????????????????? ............. // here, how do i properly fill this part??
| 00000000 ............................ Locktime

These are the parts needed to create the data for the transaction.

Everything up to the output, i understand. but i dont know what to put between the coinbase value and locktime. how does the address convert to hex? what OP codes do i need? and is this transaction layout correct?

And then I would then hash it and input it as a new transaction into the block as like so,

tx = {
  "data" : "<hex string mentioned above>",
  "hash" : "<double sha256 of data above>",
}

is that the only data i need for the json?

Thanks in advance.

discrete mathematics – Generate a value given three other values with caps

Given a Target T whose value is a positive integer. And, given a 3 integer tuple (N1, N2, N3) where the value of N1 is N1 times 2; the value of N2 is N2 times 3; and the value of N3 is N3 times 4 is there a simple way to work out all possible values for N1, N2 and N3 so that T = (N1 *2)+(N2 * 3)+(N3 * 4). Importantly N1, N2 and N3 have proscribed bounds that may be larger than the value of T. For example:

T= 123
3 int tuple = (300, 3, 200) = 1409: (600+9+800)

With this in mind, I’m seeking a way to generate all combinations of (N1, N2, N3) that will equal T. for example, where T=123, (50, 1, 5) = 123 or (0, 1, 30) = 123

Apologies if this is a really simply question. I’m trying to improve my maths.

Thanks for reading

passwords – In Hashcat, How to generate combinatior attacks consisting of more than two words (in lenght)?

I am trying to make a combinator attack using just one dictionary:

word1
word2
word3
word4
word5
...

And would like to try all 4-words-length permutations separated by commas:

word1,word2,word3,word4
word1,word3,word4,word2
word2,word3,word6,word1
...

plotting – Plot3D doesn’t generate the ellipitic paraboloid it’s supposed to

Clear("Global`*")

The default PlotRange is obscuring the detail that you want.

Plot3D((x - 1)^4 + (2 x + y)^2, {x, -8, 8}, {y, -8, 8},
 PlotRange -> {0, 6},
 ClippingStyle -> None)

enter image description here

ContourPlot((x - 1)^4 + (2 x + y)^2,
 {x, -8, 8}, {y, -8, 8},
 PlotRange -> {Full, Full, {0, 20}})

enter image description here

rsa – How do I take advantage of a specfic user’s attributes to generate an encryption key pair on the fly to encrypt a file?

Suppose I have the following 3 files with content:

file1.txt:

This is file1.txt.  It is labeled red. 

file2.txt:

This is file2.txt.  It is labeled green. 

file3.txt:

This is file3.txt.  It is labeled blue. 

Suppose these 3 files are stored on my linux file system at:

/home/shared

Finally, suppose I have 3 users who can login to this linux machine:

user1 (should only see files labeled red)
user2 (should only see files labeled green)
user3 (should only see files labeled blue)

How can I use the RSA encryption scheme to generate unique private keys for user1, user2, and user3 such that they can see the files they can decrypt the files they are supposed to see but not the ones they should not see?

Put another way, how can I generate a key pair (public/private) so that user1 can can encrypt red-labeled files and users 2 and 3 cannot decrypt file1.txt?

Ansible webserver backup: pull other host vars to generate backup scripts

I’m using Ansible to manage a fleet of dissimilar cloud linux servers. I built a script that pulls backups down to a backup server via rync over ssh. All of the host vars are managed in host_vars/example.com.yml files, each containing the following:

ansible_host: example.com
ansible_user: user
ansible_port: 22
ansible_ssh_private_key_file: "~/.ssh/id_ed25519"

They are grouped in the hosts file like so:

[webservers]
example.com
foo.com
bar.com

[backup]
backupserver.domain.com

I have a backup role for the backup group containing the backupserver.domain.com server comprising the rsync scripts, but I have to manually replicate the ansible host_vars into that backup role to generate an SSH config containing all the information already in the host_vars/*.yml files.

So my question is, within a role is it possible to iterate through the host variables of another host outside of the current host and group? Something like:

{% for item in var_hosts_group %}
Host {{ item.ansible_host }}
   Hostname {{ item.ansible_host }}
   User {{ item.ansible_user }}
   Port {{ item.ansible_port }}
   IdentityFile {{ item.ansible_ssh_private_key_file }}

{% endfor %}

Then an rsync script would be:

{% for item in var_hosts_group %}
rsync -a {{ item.ansible_host }}:/remotebackup /localbackup
{% endfor %}

This would enable me to generate the necessary templates/scripts for the webservers on the backup server and keep everything nice and DRY. Is this possible? Thanks!