One-to-one integer mapping function

前端 未结 5 1826
予麋鹿
予麋鹿 2020-12-15 23:50

We are using MySQL and developing an application where we\'d like the ID sequence not to be publicly visible... the IDs are hardly top secret and there is no significant iss

5条回答
  •  没有蜡笔的小新
    2020-12-16 00:46

    For our application, we use bit shuffle to generate the ID. It is very easy to reverse back to the original ID.

    func (m Meeting) MeetingCode() uint {
        hashed := (m.ID + 10000000) & 0x00FFFFFF
        chunks := [24]uint{}
        for i := 0; i < 24; i++ {
            chunks[i] = hashed >> i & 0x1
        }
        shuffle := [24]uint{14, 1, 15, 21, 0, 6, 5, 10, 4, 3, 20, 22, 2, 23, 8, 13, 19, 9, 18, 12, 7, 11, 16, 17}
        result := uint(0)
        for i := 0; i < 24; i++ {
            result = result | (chunks[shuffle[i]] << i)
        }
        return result
    }
    

提交回复
热议问题