Hello Friends...we r doin our UG project named Watermarking relational database using optimization based techniques....here we r supposed to use a PJW hash function to encode the values inside a database with the help of a secret key....Can anyone pls explain me the logic for this hash function and i ll be very happy if u quote it with an example....Thank you

Dani AI

Generated

asked for the PJW hash logic and an example; brief context: PJW (Peter J. Weinberger) is a non-cryptographic string hash used for symbol tables (the common ELF hash is a variant). It folds high bits back into the low bits to avoid losing information on overflow. It is not a keyed or secure MAC — do not use PJW to provide secrecy. See for background.

Algorithm (plain description): start with hash = 0. For each input byte do hash = (hash << 4) + byte. Extract the high bits with high = hash & 0xF0000000. If high != 0 then fold them in with hash ^= (high >>> 24) and clear them hash &= ~high. At the end you can return hash or a non-negative version hash & 0x7FFFFFFF, and take hash % tableSize for bucket indices. The constants correspond to OneEighth = 4, ThreeQuarters = 24 on a 32-bit word.

Short Java implementation (use bytes to match the original C behavior):

import java.nio.charset.StandardCharsets;

public static int pjwHash(byte[] data) {
    int hash = 0;
    for (byte b : data) {
        hash = (hash << 4) + (b & 0xFF);
        int high = hash & 0xF0000000;
        if (high != 0) {
            hash ^= (high >>> 24);
            hash &= ~high;
        }
    }
    return hash & 0x7FFFFFFF;
}

public static int pjwHash(String s) {
    return pjwHash(s.getBytes(StandardCharsets.UTF_8));
}

Notes and cautions: Java char is 16-bit, so iterate bytes (UTF-8) for consistent results. If a secret key is required for watermarking or integrity, use a keyed cryptographic construction (for example HMAC-SHA256) rather than trying to “salt” PJW — HMAC is designed for secrecy and integrity (HMAC). As and implied, PJW is simple to implement and good for hashing/indexing, but it is not a substitute for cryptographic primitives.

Recommended Answers

All 3 Replies

no, we're not your friends.
We're also not going to read anything that's obviously concocted by someone who doesn't care whether his/hers/its writing is at all understandable to others.
And most of all we're not here to do your homework for you.

:D hey that was great.......thanks duckman...i din want u to do my homework..i just wanted to know if anyone can teach me the logic of PJW hash function or atleast wat it does....if u din know..then u need not have replied.....

Well, you are asking someone to teach you how to do your project, which amounts to the exact same thing. Make some effort.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.