Example usage for com.google.common.hash Hashing sipHash24

List of usage examples for com.google.common.hash Hashing sipHash24

Introduction

In this page you can find the example usage for com.google.common.hash Hashing sipHash24.

Prototype

public static HashFunction sipHash24() 

Source Link

Document

Returns a hash function implementing the <a href="https://131002.net/siphash/">64-bit SipHash-2-4 algorithm</a> using a seed value of k = 00 01 02 ...

Usage

From source file:app.service.AuthService.java

protected CryptoToken newSessionToken(AccountSession session, boolean isExpire) {
    Funnel<AccountSession> sessionFunnel = isExpire ? mExpireSessionFunnel : mRefreshSessionFunnel;
    byte[] data = Hashing.sipHash24().hashObject(session, sessionFunnel).asBytes();
    String sessionString = new String(data, Charset.forName("UTF-8"));
    session.setSession(sessionString);/*from   w  ww.j av  a  2s.  c  o m*/
    CryptoToken token;
    try {
        byte[] sessionData = mObjectMapper.writeValueAsBytes(session);
        token = Crypto.encrypt(sessionData);
    } catch (Exception e) {
        logger.warn("encrypt session failure", e);
        return null;
    }
    return token;
}

From source file:app.service.AuthService.java

protected AccountSession getSessionWithCheck(CryptoToken cryptoToken, boolean isExpire) {
    Funnel<AccountSession> sessionFunnel = isExpire ? mExpireSessionFunnel : mRefreshSessionFunnel;
    AccountSession session;/*ww w.  j  a  va2 s .c o  m*/
    try {
        byte[] json = Crypto.decrypt(cryptoToken);
        session = mObjectMapper.readValue(json, AccountSession.class);
    } catch (Exception e) {
        logger.warn("decrypt token failure", e);
        return null;
    }

    byte[] sessionData = Hashing.sipHash24().hashObject(session, sessionFunnel).asBytes();
    String sessionString = new String(sessionData, StandardCharsets.UTF_8);
    if (!sessionString.equals(session.getSession())) {
        logger.warn("session not equal");
        return null;
    }

    return session;
}