piątek, 26 lutego 2016

Linear feedback shift register

I want to introduce small pseudo random and cryptography series. It will includes lfsr shift registers and pseudorandom generators based on it. After all I will show implementation of A5/1 cipher used in GSM. Code of course is not optimised, but it's easy to understand how it works and that's the point.

Linear Feedback Shift Register (LFSR) . It is a shift register whose input bit is a linear function of its previous state . Typically, it is an essential component of a more complex algorithms. During one cycle of work algorithm calculated by bit XOR operations , it is sent to the first place on the left . The remaining bits are shifted to the right by one place. Bit which became the last position of the shift becomes another bit generated in the LFSR. If the algorithm is implemented in hardware , it generates bits very quickly. Its cryptographic properties but leave much to be desired . Cryptographic value is poor because the knowledge of 2n consecutive bits within allows to find the value generated from that point.

So here's the implementation:

bool stage[5] = {true, true, true};

int reg1[20] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int reg2[20] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int reg3[20] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

int lfsr_1() {
    if (stage[0]) {
        stage[0] = false;
        return reg1[0];
    }
    int n = 18, temp = reg1[6] ^reg1[17];   //1+x7+x18
    int k = 0;
    for (k = n - 1; k > 0; k--) reg1[k] = reg1[k - 1];
    reg1[0] = temp;
    return temp;
}

int lfsr_2() {
    if (stage[1]) {
        stage[1] = false;
        return reg2[0];
    }
    int n = 19, temp = reg2[0] ^reg2[1] ^reg2[4] ^reg2[18]; //1+x+x2+x5+x19
    int k = 0;
    for (k = n - 1; k > 0; k--) reg2[k] = reg2[k - 1];
    reg2[0] = temp;
    return temp;
}

int lfsr_3() {
    if (stage[2]) {
        stage[2] = false;
        return reg3[0];
    }
    int n = 20, temp = reg3[2] ^reg3[19];   //1+x3+x20
    int k = 0;
    for (k = n - 1; k > 0; k--) reg3[k] = reg3[k - 1];
    reg3[0] = temp;
    return temp;
}
This is the base for rest of generators Shrinker, Geffe and Stop n go:
https://github.com/khipis/c-unix-sandbox/blob/master/shift-registers/lfsr.h

niedziela, 21 lutego 2016

Premium sms Java integration

Last time I had to integrate application with sms premium payments.
I couldn't find any solution for easy set up and running, so I was forced to write my own client.

Here's the result, feel free to use this:

Library which integrates with premium-sms : https://platnosci-online.pl/panel.html
Sample usage:

Config config = ConfigBuilder.instance()
           .key("YOUR KEY")
           .partnerId("YOUR PARTNER ID")
           .url("https://platnosci-online.pl/sms_check.php")
           .build();


CodeQuery codeQuery = CodeQueryBuilder.instance()
           .suffix("YOUR SUFFIX")
           .code("USER ENTERED CODE")
           .cost(100)
           .build();

CodeVerifier verifier = new CodeVerifier(config);
        
if(verifier.verify(codeQuery).getVerificationStatus()
           .equals(VerificationStatus.SUCCESS)) 
{
            //Give profit's for user
}

That's all! :)

Go to: https://github.com/khipis/premiumsms

środa, 17 lutego 2016

Dining philosophers, counting semaphore solution, C POSIX THREADS Example

One of the solution in dining philosopher problem is to set up counting semaphore, which eliminates the phenomenon of deadlock, because only 4 philosophers at a time can compete for the chopsticks, so it will eliminate a situation that all philosophers will raise at once left chopstick.
sem_t sem;

void *philosopher_semaphore(void *p) {
    struct philosopher_t philosopher = *((struct philosopher_t *) (p));
    while (1) {
        sem_wait(&sem); // Semaphore up
        if (!pthread_mutex_lock(&chopstick[philosopher.left])) {
            printf("%d pick up's left chopstick \n", philosopher.nr);
            while (1) {
                if (!pthread_mutex_lock(&chopstick[philosopher.right])) {
                    printf("%d pick up's right chopstick, happy bastard, EAT \n", philosopher.nr);
                    pthread_mutex_unlock(&chopstick[philosopher.left]);
                    pthread_mutex_unlock(&chopstick[philosopher.right]);
                    break;
                }
            }
        }
        sem_post(&sem); //Semaphore down
    }
}

int main() {

    sem_init(&sem, 0, 4); //Initialization of semaphore counting to 4
...
Now we have the real concurrency:
1 pick up's right chopstick, happy bastard, EAT 
1 pick up's left chopstick 
1 pick up's right chopstick, happy bastard, EAT 
1 pick up's left chopstick 
1 pick up's right chopstick, happy bastard, EAT 
2 pick up's left chopstick 
2 pick up's right chopstick, happy bastard, EAT 
3 pick up's left chopstick 
3 pick up's right chopstick, happy bastard, EAT 
2 pick up's left chopstick 
2 pick up's right chopstick, happy bastard, EAT 
0 pick up's left chopstick 
0 pick up's right chopstick, happy bastard, EAT 
Happy coding!
Full code here: https://github.com/khipis/c-unix-sandbox/blob/master/philosophers/philosophers_semaphore.c

wtorek, 16 lutego 2016

Dining philosophers, starvation example in C POSIX THREADS

In the last post I implemented dining philosophers problem with deadlock. Let's try to fix it by adding one asymmetric philosopher. This one picks up as first right chopstick instead of left.

The change is simple:
    
    p4_t->nr = 4;
    p4_t->left = 0;
    p4_t->right = 4;
Yeah finally we don't have deadlocks in our multithread enterprise software :)
But wait! One philosopher becomes fatter and fatter, when others are very slim...Im not good at jokes.

It's happening because of unfair access. No one picks up chopstick of the asymmetric philosoper in the first try. "Starvation is a problem encountered in concurrent computing where a process is perpetually denied necessary resources to process its work." - Wikipedia

Now we must face the starvation problem.

4 pick up's right chopstick, happy bastard, EAT 
4 pick up's left chopstick 
4 pick up's right chopstick, happy bastard, EAT 
4 pick up's left chopstick 
4 pick up's right chopstick, happy bastard, EAT 
4 pick up's left chopstick 
4 pick up's right chopstick, happy bastard, EAT 
4 pick up's left chopstick 
4 pick up's right chopstick, happy bastard, EAT 


The situation is better when we add more asymmetric philosophers:
    p0_t->nr = 0;
    p0_t->left = 1;
    p0_t->right = 0;

    p1_t->nr = 1;
    p1_t->left = 1;
    p1_t->right = 2;

    p2_t->nr = 2;
    p2_t->left = 3;
    p2_t->right = 2;

    p3_t->nr = 3;
    p3_t->left = 3;
    p3_t->right = 4;

    p4_t->nr = 4;
    p4_t->left = 4;
    p4_t->right = 0;
4 pick up's right chopstick, happy bastard, EAT 
1 pick up's right chopstick, happy bastard, EAT 
2 pick up's left chopstick 
2 pick up's right chopstick, happy bastard, EAT 
But the behavior is unpredictable.
Full code here: https://github.com/khipis/c-unix-sandbox/blob/master/philosophers/philosophers_starvation.c

piątek, 12 lutego 2016

Dining philosophers, deadlock example in C POSIX THREADS

I would like to introduce my series of C sandbox stuff. Only for fun, definitely no-production and not clean code :)

I tried to implement dining philosophers problem in perspective of black scenarios. First on the list is deadlock. According to wikipedia "...deadlock is a situation in which two or more competing actions are each waiting for the other to finish, and thus neither ever does."

So let's start, here's the sample philosopher code:
void *philosopher_deadlock(void *p) {
    struct philosopher_t philosopher = *((struct philosopher_t *) (p));
    while (1) {
        if (!pthread_mutex_lock(&chopstick[philosopher.left])) {
            printf("%d pick up's left chopstick \n", philosopher.nr);
            while (1) {
                if (!pthread_mutex_lock(&chopstick[philosopher.right])) {
                    printf("%d pick up's right chopstick, happy bastard, EAT \n", philosopher.nr);
                    pthread_mutex_unlock(&chopstick[philosopher.left]);
                    pthread_mutex_unlock(&chopstick[philosopher.right]);
                    break;
                }
            }
        }
    }
}



After a seconds of execution we have a deadlock situation:
1 pick up's left chopstick 
1 pick up's right chopstick, happy bastard, EAT 
1 pick up's left chopstick 
1 pick up's right chopstick, happy bastard, EAT 
1 pick up's left chopstick 
2 pick up's left chopstick 
4 pick up's left chopstick 
3 pick up's left chopstick 
Full code here: https://github.com/khipis/c-unix-sandbox/blob/master/philosophers/philosophers_deadlock.c

niedziela, 7 lutego 2016

Scrolling textarea to selected text. Pure JavaScript

Really simple task but all solutions I found over internet have some disanvantages.
My also has one: inefficient. But works well in every case and it's written in pure JavaScript.

I stucked it together from all snippets I've seen.

If You know any better solution (without counting chars, pixel, sending keyups and others, not everytime working stuff, please leave comment with code snippet :)

function scrollToPosition(position, textarea) {
  var text = textarea.value;
  var textBeforePosition = text.substr(0, position);
  textarea.value = textBeforePosition;
  textarea.focus();
  textarea.value = text;
  textarea.setSelectionRange(position,position+10);  
  textarea.focus();
} 

poniedziałek, 1 lutego 2016

CQ5, AEM6 real price


When I was reading
Why are Mid-Market Companies Still Looking at Adobe Experience Manager?

I have only one tought. Is $450,000 - $2 000 000 the final price?

I think the price is much bigger. You have to think about the specialists who are expensive and hard to find with boxed solutions. Usually you must train some but not all employers think about it cause mostly they think "I pay so much for a system so propably it's safe, efficient etc.". And definitely its self configurable and easy to maintain :)

In my opinion it costs much more.

Evidence?

inurl:/core/content/login

Wtf? Servers configuration issues? You can say it's only a login page...

but...

AEM6 security checklist
CQ5 security checklist

Now imagine opposite situation, when attacker will go trough "security checklist" :)

Results are terrible. $450,000 it's just small change.